|
74 | 74 | IntExpr, FloatExpr, UnicodeExpr, TempNode, OverloadPart, |
75 | 75 | PlaceholderNode, COVARIANT, CONTRAVARIANT, INVARIANT, |
76 | 76 | nongen_builtins, get_member_expr_fullname, REVEAL_TYPE, |
77 | | - REVEAL_LOCALS, is_final_node, TypedDictExpr, type_aliases_target_versions, |
| 77 | + REVEAL_LOCALS, is_final_node, TypedDictExpr, type_aliases_source_versions, |
78 | 78 | EnumCallExpr, RUNTIME_PROTOCOL_DECOS, FakeExpression, Statement, AssignmentExpr, |
79 | 79 | ParamSpecExpr |
80 | 80 | ) |
@@ -307,12 +307,27 @@ def prepare_typing_namespace(self, file_node: MypyFile) -> None: |
307 | 307 |
|
308 | 308 | They will be replaced with real aliases when corresponding targets are ready. |
309 | 309 | """ |
310 | | - for stmt in file_node.defs.copy(): |
311 | | - if (isinstance(stmt, AssignmentStmt) and len(stmt.lvalues) == 1 and |
312 | | - isinstance(stmt.lvalues[0], NameExpr)): |
313 | | - # Assignment to a simple name, remove it if it is a dummy alias. |
314 | | - if 'typing.' + stmt.lvalues[0].name in type_aliases: |
315 | | - file_node.defs.remove(stmt) |
| 310 | + # This is all pretty unfortunate. typeshed now has a |
| 311 | + # sys.version_info check for OrderedDict, and we shouldn't |
| 312 | + # take it out, because it is correct and a typechecker should |
| 313 | + # use that as a source of truth. But instead we rummage |
| 314 | + # through IfStmts to remove the info first. (I tried to |
| 315 | + # remove this whole machinery and ran into issues with the |
| 316 | + # builtins/typing import cycle.) |
| 317 | + def helper(defs: List[Statement]) -> None: |
| 318 | + for stmt in defs.copy(): |
| 319 | + if isinstance(stmt, IfStmt): |
| 320 | + for body in stmt.body: |
| 321 | + helper(body.body) |
| 322 | + if stmt.else_body: |
| 323 | + helper(stmt.else_body.body) |
| 324 | + if (isinstance(stmt, AssignmentStmt) and len(stmt.lvalues) == 1 and |
| 325 | + isinstance(stmt.lvalues[0], NameExpr)): |
| 326 | + # Assignment to a simple name, remove it if it is a dummy alias. |
| 327 | + if 'typing.' + stmt.lvalues[0].name in type_aliases: |
| 328 | + defs.remove(stmt) |
| 329 | + |
| 330 | + helper(file_node.defs) |
316 | 331 |
|
317 | 332 | def prepare_builtins_namespace(self, file_node: MypyFile) -> None: |
318 | 333 | """Add certain special-cased definitions to the builtins module. |
@@ -430,7 +445,7 @@ def add_builtin_aliases(self, tree: MypyFile) -> None: |
430 | 445 | """ |
431 | 446 | assert tree.fullname == 'typing' |
432 | 447 | for alias, target_name in type_aliases.items(): |
433 | | - if type_aliases_target_versions[alias] > self.options.python_version: |
| 448 | + if type_aliases_source_versions[alias] > self.options.python_version: |
434 | 449 | # This alias is not available on this Python version. |
435 | 450 | continue |
436 | 451 | name = alias.split('.')[-1] |
|
0 commit comments