Skip to content

Commit

Permalink
Fix TypedDictType crash with mypy 1.12.x (#2408)
Browse files Browse the repository at this point in the history
* Fix TypedDictType crash with mypy 1.12.x

* Add missing generic parameter for builtins.memoryview
  • Loading branch information
federicobond authored Oct 16, 2024
1 parent 715df65 commit c9c7290
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
12 changes: 10 additions & 2 deletions mypy_django_plugin/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,21 @@ def convert_any_to_type(typ: MypyType, referred_to_type: MypyType) -> MypyType:


def make_typeddict(
api: Union[SemanticAnalyzer, CheckerPluginInterface], fields: Dict[str, MypyType], required_keys: Set[str]
api: Union[SemanticAnalyzer, CheckerPluginInterface],
fields: Dict[str, MypyType],
required_keys: Set[str],
readonly_keys: Set[str],
) -> TypedDictType:
if isinstance(api, CheckerPluginInterface):
fallback_type = api.named_generic_type("typing._TypedDict", [])
else:
fallback_type = api.named_type("typing._TypedDict", [])
typed_dict_type = TypedDictType(fields, required_keys=required_keys, fallback=fallback_type)
typed_dict_type = TypedDictType(
fields,
required_keys=required_keys,
readonly_keys=readonly_keys,
fallback=fallback_type,
)
return typed_dict_type


Expand Down
1 change: 1 addition & 0 deletions mypy_django_plugin/transformers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ def get_annotated_type(
api,
fields={**annotations.items, **fields_dict.items},
required_keys={*annotations.required_keys, *fields_dict.required_keys},
readonly_keys={*annotations.readonly_keys, *fields_dict.readonly_keys},
)
else:
annotated_model = helpers.lookup_fully_qualified_typeinfo(api, model_type.type.fullname + "@AnnotatedWith")
Expand Down
9 changes: 7 additions & 2 deletions mypy_django_plugin/transformers/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,12 @@ def extract_proper_type_queryset_annotate(ctx: MethodContext, django_context: Dj

fields_dict = None
if field_types is not None:
fields_dict = helpers.make_typeddict(api, fields=field_types, required_keys=set(field_types.keys()))
fields_dict = helpers.make_typeddict(
api,
fields=field_types,
required_keys=set(field_types.keys()),
readonly_keys=set(),
)

if fields_dict is not None:
annotated_type = get_annotated_type(api, model_type, fields_dict=fields_dict)
Expand Down Expand Up @@ -349,5 +354,5 @@ def extract_proper_type_queryset_values(ctx: MethodContext, django_context: Djan

column_types[field_lookup] = field_lookup_type

row_type = helpers.make_typeddict(ctx.api, column_types, set(column_types.keys()))
row_type = helpers.make_typeddict(ctx.api, column_types, set(column_types.keys()), set())
return helpers.reparametrize_instance(default_return_type, [model_type, row_type])
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ Django==5.1.2; python_version >= '3.10'
-e .[redis,compatible-mypy,oracle]

# Overrides:
mypy==1.11.2
mypy==1.12.0
pyright==1.1.384
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def find_stub_files(name: str) -> List[str]:

# Keep compatible-mypy major.minor version pinned to what we use in CI (requirements.txt)
extras_require = {
"compatible-mypy": ["mypy~=1.11.0"],
"compatible-mypy": ["mypy~=1.12.0"],
"redis": ["redis"],
"oracle": ["oracledb"],
}
Expand Down
2 changes: 1 addition & 1 deletion tests/typecheck/fields/test_base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
message = models.BinaryField()
obj = EncodedMessage(b'\x010')
reveal_type(obj.message) # N: Revealed type is "Union[builtins.bytes, builtins.memoryview]"
reveal_type(obj.message) # N: Revealed type is "Union[builtins.bytes, builtins.memoryview[builtins.int]]"
- case: test_small_auto_field_class_presents_as_int
main: |
Expand Down

0 comments on commit c9c7290

Please sign in to comment.