Skip to content

Add typing.OrderedDict to type_aliases #9389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ def get_column(self) -> int:
'typing.Counter': 'collections.Counter',
'typing.DefaultDict': 'collections.defaultdict',
'typing.Deque': 'collections.deque',
'typing.OrderedDict': 'collections.OrderedDict',
} # type: Final

# This keeps track of the oldest supported Python version where the corresponding
# alias _target_ is available.
type_aliases_target_versions = {
# alias source is available.
type_aliases_source_versions = {
'typing.List': (2, 7),
'typing.Dict': (2, 7),
'typing.Set': (2, 7),
Expand All @@ -127,6 +128,7 @@ def get_column(self) -> int:
'typing.Counter': (2, 7),
'typing.DefaultDict': (2, 7),
'typing.Deque': (2, 7),
'typing.OrderedDict': (3, 7),
} # type: Final

reverse_builtin_aliases = {
Expand All @@ -139,6 +141,8 @@ def get_column(self) -> int:
nongen_builtins = {'builtins.tuple': 'typing.Tuple',
'builtins.enumerate': ''} # type: Final
nongen_builtins.update((name, alias) for alias, name in type_aliases.items())
# Drop OrderedDict from this for backward compatibility
del nongen_builtins['collections.OrderedDict']

RUNTIME_PROTOCOL_DECOS = ('typing.runtime_checkable',
'typing_extensions.runtime',
Expand Down
31 changes: 23 additions & 8 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
IntExpr, FloatExpr, UnicodeExpr, TempNode, OverloadPart,
PlaceholderNode, COVARIANT, CONTRAVARIANT, INVARIANT,
nongen_builtins, get_member_expr_fullname, REVEAL_TYPE,
REVEAL_LOCALS, is_final_node, TypedDictExpr, type_aliases_target_versions,
REVEAL_LOCALS, is_final_node, TypedDictExpr, type_aliases_source_versions,
EnumCallExpr, RUNTIME_PROTOCOL_DECOS, FakeExpression, Statement, AssignmentExpr,
ParamSpecExpr
)
Expand Down Expand Up @@ -307,12 +307,27 @@ def prepare_typing_namespace(self, file_node: MypyFile) -> None:

They will be replaced with real aliases when corresponding targets are ready.
"""
for stmt in file_node.defs.copy():
if (isinstance(stmt, AssignmentStmt) and len(stmt.lvalues) == 1 and
isinstance(stmt.lvalues[0], NameExpr)):
# Assignment to a simple name, remove it if it is a dummy alias.
if 'typing.' + stmt.lvalues[0].name in type_aliases:
file_node.defs.remove(stmt)
# This is all pretty unfortunate. typeshed now has a
# sys.version_info check for OrderedDict, and we shouldn't
# take it out, because it is correct and a typechecker should
# use that as a source of truth. But instead we rummage
# through IfStmts to remove the info first. (I tried to
# remove this whole machinery and ran into issues with the
# builtins/typing import cycle.)
def helper(defs: List[Statement]) -> None:
for stmt in defs.copy():
if isinstance(stmt, IfStmt):
for body in stmt.body:
helper(body.body)
if stmt.else_body:
helper(stmt.else_body.body)
if (isinstance(stmt, AssignmentStmt) and len(stmt.lvalues) == 1 and
isinstance(stmt.lvalues[0], NameExpr)):
# Assignment to a simple name, remove it if it is a dummy alias.
if 'typing.' + stmt.lvalues[0].name in type_aliases:
defs.remove(stmt)

helper(file_node.defs)

def prepare_builtins_namespace(self, file_node: MypyFile) -> None:
"""Add certain special-cased definitions to the builtins module.
Expand Down Expand Up @@ -430,7 +445,7 @@ def add_builtin_aliases(self, tree: MypyFile) -> None:
"""
assert tree.fullname == 'typing'
for alias, target_name in type_aliases.items():
if type_aliases_target_versions[alias] > self.options.python_version:
if type_aliases_source_versions[alias] > self.options.python_version:
# This alias is not available on this Python version.
continue
name = alias.split('.')[-1]
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy