Skip to content

Commit

Permalink
Prevent ChainMap, Counter and OrderedDict from being dropped if…
Browse files Browse the repository at this point in the history
… they're only imported.

e.g. for a file that only contains:
```py
from collections import OrderedDict
```

Pytype will completely drop `OrderedDict`, because that class is aliased to `typing.OrderedDict`.

PiperOrigin-RevId: 589215092
  • Loading branch information
Solumin authored and rchen152 committed Dec 14, 2023
1 parent c1c440c commit 222e7c3
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pytype/pytd/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ def _DropTypingConstant(self, node):
if self.class_names or node.value:
return False
full_typing_name = f"typing.{node.name}"
# TODO(b/315507078): This is only necessary while these three classes
# are aliases of typing members.
if full_typing_name in (
"typing.ChainMap",
"typing.Counter",
"typing.OrderedDict",
):
return False
if node.type == f"Type[{full_typing_name}]":
self._imports.add(full_typing_name, node.name)
self._imports.decrement_typing_count("Type")
Expand Down

0 comments on commit 222e7c3

Please sign in to comment.