Skip to content

CharField primary key incorrectly inferred as int in abstract model inheritance in django #2218

@cobel1024

Description

@cobel1024

Bug Report

Description

Pyrefly consistently infers id as int type across my entire project, even when explicitly defined as CharField(primary_key=True) in abstract models.

Observations

Screenshot 1: CharField inheritance - Works correctly (no errors)

Image

Both StrIdMixin and StrIdChildModel inherit CharField as primary key.

  • test_str() with string operations: No errors shown
  • test_int() with integer operations: Correctly shows errors with # error: comments

Screenshot 2: CharField inheritance - Does NOT work (false errors)

Image

TuidMixin also defines id = CharField(...) with primary_key=True.

  • Pyrefly shows errors on string operations
  • Treats id as int instead of str

Problem

I'm not sure what conditions cause the difference, but in my project, Pyrefly consistently infers id as int regardless of the actual CharField definition. The behavior is inconsistent between the two cases shown.

https://github.com/cobel1024/minima/blob/f63de3d7d96a33c522bb78b15b96e48dd4cd7dfb/core/apps/exam/models.py#L178

My workaround is if TYPE_CHECKING: pk: str

Environment

  • Pyrefly version: 0.49
  • Django version: 6

Expected

CharField primary key should always be typed as str, consistently across all abstract model inheritance patterns.

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions