Skip to content
This repository was archived by the owner on Jun 26, 2026. It is now read-only.

Commit f89aee0

Browse files
shannonzhufacebook-github-bot
authored andcommitted
Register type aliases typed as Any
Summary: EDIT: first version was based on wrong assumptions - aliases and globals are separate concepts, differentiated by whether there is an explicit annotation python/mypy#3494 ----- Some examples where we rely on globals typed as `Any` (they might have some other value on the RHS of the assign) to be valid types: diffusion/IGSRVHG/browse/master/distillery/gatekeeper/restraints/checks/request/app_os.py$36,45# https://fburl.com/biggrep/rmd6ufa4 Typeshed mock.pyi uses this for some of its classes: https://github.com/python/typeshed/blob/0730fe5fcb269df3f1b72e2544671fec1498a6db/stdlib/3/unittest/mock.pyi Reviewed By: sinancepel Differential Revision: D13858398 fbshipit-source-id: 290c5d9644c88c52082c58c052ad9a8efe172d64
1 parent e2999b8 commit f89aee0

4 files changed

Lines changed: 90 additions & 27 deletions

File tree

‎analysis/environment.ml‎

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -434,48 +434,72 @@ let register_aliases (module Handler: Handler) sources =
434434
let order = (module Handler.TypeOrderHandler : TypeOrder.Handler) in
435435
let collect_aliases { Source.handle; statements; qualifier; _ } =
436436
let rec visit_statement ~qualifier ?(in_class_body = false) aliases { Node.value; _ } =
437-
let is_allowable_alias_annotation = function
438-
| Some {
439-
Node.value = Access
440-
(SimpleAccess [
441-
Access.Identifier "typing";
442-
Access.Identifier "Type";
443-
Access.Identifier "__getitem__";
444-
Access.Call _;
445-
]);
446-
_;
447-
} ->
448-
true
449-
| None ->
450-
true
451-
| _ ->
452-
false
453-
in
454437
match value with
455438
| Assign {
456439
Assign.target = { Node.value = Access (SimpleAccess target); _ };
457440
annotation;
458441
value;
459442
_;
460-
}
461-
when is_allowable_alias_annotation annotation ->
443+
} ->
462444
let target =
463445
let access = Access.delocalize_qualified target in
464446
if in_class_body then
465447
access
466448
else
467449
qualifier @ access
468450
in
451+
let target_annotation =
452+
Type.create
453+
~aliases:Handler.aliases
454+
(Node.create (Access (SimpleAccess target)) ~location:(Node.location value))
455+
in
469456
begin
470-
match Node.value value with
471-
| Access _ ->
457+
match Node.value value, annotation with
458+
| _, Some {
459+
Node.value =
460+
Access
461+
(SimpleAccess [
462+
Access.Identifier "typing";
463+
Access.Identifier "Type";
464+
Access.Identifier "__getitem__";
465+
Access.Call {
466+
Node.value = [{
467+
Argument.value = ({
468+
Node.value = Access (SimpleAccess [
469+
Access.Identifier "mypy_extensions";
470+
Access.Identifier "TypedDict";
471+
Access.Identifier "__getitem__";
472+
Access.Call _;
473+
]);
474+
_;
475+
} as annotation);
476+
_;
477+
}];
478+
_;
479+
};
480+
]);
481+
_;
482+
} ->
483+
if not (Type.equal target_annotation Type.Top) then
484+
(handle, target, annotation) :: aliases
485+
else
486+
aliases
487+
| _, Some ({
488+
Node.value =
489+
Access
490+
(SimpleAccess[
491+
Access.Identifier "typing";
492+
Access.Identifier "Any";
493+
]);
494+
_;
495+
} as annotation) ->
496+
if not (Type.equal target_annotation Type.Top) then
497+
(handle, target, annotation) :: aliases
498+
else
499+
aliases
500+
| Access _, None ->
472501
let value = Expression.delocalize value in
473502
let value_annotation = Type.create ~aliases:Handler.aliases value in
474-
let target_annotation =
475-
Type.create
476-
~aliases:Handler.aliases
477-
(Node.create (Access (SimpleAccess target)) ~location:(Node.location value))
478-
in
479503
if not (Type.equal target_annotation Type.Top ||
480504
Type.equal value_annotation Type.Top ||
481505
Type.equal value_annotation target_annotation) then

‎analysis/test/environmentTest.ml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,28 @@ let test_register_aliases _ =
459459
|};
460460
]
461461
["x.C", "x.C"];
462+
assert_resolved
463+
[
464+
parse
465+
{|
466+
A = int
467+
B: typing.Type[int] = int
468+
C: typing.Type[int]
469+
D: typing.Any
470+
E = ...
471+
F = A
472+
G = 1
473+
|};
474+
]
475+
[
476+
"A", "int";
477+
"B", "B";
478+
"C", "C";
479+
"D", "typing.Any";
480+
"E", "E";
481+
"F", "int";
482+
"G", "G";
483+
];
462484
()
463485
464486

‎analysis/test/integration/annotationTest.ml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,17 @@ let test_check_undefined_type _ =
172172
let test_check_invalid_type _ =
173173
assert_type_errors
174174
{|
175-
MyType: typing.Type[int] = int
175+
MyType = int
176176
x: MyType = 1
177177
|}
178178
[];
179+
assert_type_errors
180+
{|
181+
# Type aliases cannot be annotated
182+
MyType: typing.Type[int] = int
183+
x: MyType = 1
184+
|}
185+
["Invalid type [31]: Expression `MyType` is not a valid type."];
179186
assert_type_errors
180187
{|
181188
x: MyType = 1
@@ -200,6 +207,12 @@ let test_check_invalid_type _ =
200207
MyType: typing.Any
201208
x: MyType = 1
202209
|}
210+
[];
211+
assert_type_errors
212+
{|
213+
MyType: typing.Any
214+
x: typing.List[MyType] = [1]
215+
|}
203216
[]
204217

205218

‎analysis/test/typeCheckTest.ml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ let test_check_annotation _ =
228228
assert_check_annotation
229229
"x: typing.Type[int] = int"
230230
"x"
231+
["Invalid type [31]: Expression `x` is not a valid type."];
232+
assert_check_annotation
233+
"x = int"
234+
"x"
231235
[];
232236
assert_check_annotation
233237
"x: typing.Any"

0 commit comments

Comments
 (0)