Skip to content

Commit 6d3dd30

Browse files
federicobondasottile-sentry
authored andcommitted
cherry-pick mypy 1.12.x support
upstream PR typeddjango#2408
1 parent 4d3af0b commit 6d3dd30

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

mypy_django_plugin/lib/helpers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,21 @@ def convert_any_to_type(typ: MypyType, referred_to_type: MypyType) -> MypyType:
396396

397397

398398
def make_typeddict(
399-
api: Union[SemanticAnalyzer, CheckerPluginInterface], fields: Dict[str, MypyType], required_keys: Set[str]
399+
api: Union[SemanticAnalyzer, CheckerPluginInterface],
400+
fields: Dict[str, MypyType],
401+
required_keys: Set[str],
402+
readonly_keys: Set[str],
400403
) -> TypedDictType:
401404
if isinstance(api, CheckerPluginInterface):
402405
fallback_type = api.named_generic_type("typing._TypedDict", [])
403406
else:
404407
fallback_type = api.named_type("typing._TypedDict", [])
405-
typed_dict_type = TypedDictType(fields, required_keys=required_keys, fallback=fallback_type)
408+
typed_dict_type = TypedDictType(
409+
fields,
410+
required_keys=required_keys,
411+
readonly_keys=readonly_keys,
412+
fallback=fallback_type,
413+
)
406414
return typed_dict_type
407415

408416

mypy_django_plugin/transformers/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ def get_annotated_type(
11831183
api,
11841184
fields={**annotations.items, **fields_dict.items},
11851185
required_keys={*annotations.required_keys, *fields_dict.required_keys},
1186+
readonly_keys={*annotations.readonly_keys, *fields_dict.readonly_keys},
11861187
)
11871188
else:
11881189
annotated_model = helpers.lookup_fully_qualified_typeinfo(api, model_type.type.fullname + "@AnnotatedWith")

mypy_django_plugin/transformers/querysets.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,12 @@ def extract_proper_type_queryset_annotate(ctx: MethodContext, django_context: Dj
275275

276276
fields_dict = None
277277
if field_types is not None:
278-
fields_dict = helpers.make_typeddict(api, fields=field_types, required_keys=set(field_types.keys()))
278+
fields_dict = helpers.make_typeddict(
279+
api,
280+
fields=field_types,
281+
required_keys=set(field_types.keys()),
282+
readonly_keys=set(),
283+
)
279284

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

350355
column_types[field_lookup] = field_lookup_type
351356

352-
row_type = helpers.make_typeddict(ctx.api, column_types, set(column_types.keys()))
357+
row_type = helpers.make_typeddict(ctx.api, column_types, set(column_types.keys()), set())
353358
return helpers.reparametrize_instance(default_return_type, [model_type, row_type])

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ Django==5.1.1; python_version >= '3.10'
1414
-e .[redis,compatible-mypy,oracle]
1515

1616
# Overrides:
17-
mypy==1.11.2
18-
pyright==1.1.381
17+
mypy==1.12.0
18+
pyright==1.1.384

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def find_stub_files(name: str) -> List[str]:
3232

3333
# Keep compatible-mypy major.minor version pinned to what we use in CI (requirements.txt)
3434
extras_require = {
35-
"compatible-mypy": ["mypy~=1.11.0"],
35+
"compatible-mypy": ["mypy~=1.12.0"],
3636
"redis": ["redis"],
3737
"oracle": ["oracledb"],
3838
}

tests/typecheck/fields/test_base.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
message = models.BinaryField()
172172
obj = EncodedMessage(b'\x010')
173173
174-
reveal_type(obj.message) # N: Revealed type is "Union[builtins.bytes, builtins.memoryview]"
174+
reveal_type(obj.message) # N: Revealed type is "Union[builtins.bytes, builtins.memoryview[builtins.int]]"
175175
176176
- case: test_small_auto_field_class_presents_as_int
177177
main: |

0 commit comments

Comments
 (0)