Skip to content

Commit

Permalink
Merge pull request #289 from cdce8p/typing-rewrite-special-cases
Browse files Browse the repository at this point in the history
Update typing rewrite with special cases
  • Loading branch information
asottile authored Jul 19, 2022
2 parents b826a6c + 9bac355 commit c6d5e6c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
13 changes: 7 additions & 6 deletions reorder_python_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def _fix_file(
# Using:
# flake8-typing-imports==1.12.0
# mypy_extensions==0.4.3
# typing_extensions==4.0.1
# typing_extensions==4.3.0
REPLACES[(3, 7)].update((
'mypy_extensions=typing:NoReturn',
))
Expand All @@ -441,25 +441,21 @@ def _fix_file(
'typing_extensions=typing:Text',
'typing_extensions=typing:Type',
'typing_extensions=typing:get_type_hints',
'typing_extensions=typing:overload',
))
REPLACES[(3, 7)].update((
'typing_extensions=typing:AsyncContextManager',
'typing_extensions=typing:AsyncGenerator',
'typing_extensions=typing:ChainMap',
'typing_extensions=typing:Counter',
'typing_extensions=typing:Deque',
'typing_extensions=typing:NoReturn',
))
REPLACES[(3, 8)].update((
'typing_extensions=typing:Final',
'typing_extensions=typing:Literal',
'typing_extensions=typing:OrderedDict',
'typing_extensions=typing:Protocol',
'typing_extensions=typing:SupportsIndex',
'typing_extensions=typing:TypedDict',
'typing_extensions=typing:final',
'typing_extensions=typing:get_args',
'typing_extensions=typing:get_origin',
'typing_extensions=typing:runtime_checkable',
))
REPLACES[(3, 9)].update((
Expand All @@ -468,8 +464,13 @@ def _fix_file(
REPLACES[(3, 10)].update((
'typing_extensions=typing:Concatenate',
'typing_extensions=typing:ParamSpec',
'typing_extensions=typing:ParamSpecArgs',
'typing_extensions=typing:ParamSpecKwargs',
'typing_extensions=typing:TypeAlias',
'typing_extensions=typing:TypeGuard',
'typing_extensions=typing:get_args',
'typing_extensions=typing:get_origin',
'typing_extensions=typing:is_typeddict',
))
# END GENERATED

Expand Down
28 changes: 23 additions & 5 deletions testing/generate-typing-rewrite-info
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ else:
from importlib_metadata import version


# --- typing_extensions notes ---
# https://github.com/python/typing_extensions#other-notes-and-limitations
# - get_origin and get_args lack support for Annotated in Python 3.8
# and lack support for ParamSpecArgs and ParamSpecKwargs in 3.9.
# - Starting with 3.11, NamedTuple and TypedDict can inherit from Generic
# - @final was changed in Python 3.11 to set the .__final__ attribute
# - @overload was changed in Python 3.11 to make function overloads
# introspectable at runtime.
CUSTOM_TYPING_EXT_SYMBOLS = {
(3, 10): {'get_origin', 'get_args'},
(3, 11): {'NamedTuple', 'TypedDict', 'final', 'overload'},
}


def main() -> int:
flake8_typing_imports_version = version('flake8-typing-imports')
mypy_extensions_version = version('mypy_extensions')
Expand All @@ -23,7 +37,9 @@ def main() -> int:
mypy_extensions_all = frozenset(
a for a in dir(mypy_extensions) if a != 'Any'
)
typing_extensions_all = frozenset(typing_extensions.__all__)
typing_extensions_all = frozenset(typing_extensions.__all__) - {
sym for v in CUSTOM_TYPING_EXT_SYMBOLS.values() for sym in v
}

# some attrs are removed and then added back
min_contiguous_versions: dict[str, flake8_typing_imports.Version] = {}
Expand Down Expand Up @@ -53,11 +69,13 @@ def main() -> int:
deltas[v] = attrs - prev
prev = attrs

mypy_extensions_added: dict[tuple[int, int], list[str]] = {}
typing_extensions_added: dict[tuple[int, int], list[str]] = {}
mypy_extensions_added: dict[tuple[int, int], set[str]] = {}
typing_extensions_added: dict[tuple[int, int], set[str]] = defaultdict(set)
for v, attrs in deltas.items():
mypy_extensions_added[v] = sorted(attrs & mypy_extensions_all)
typing_extensions_added[v] = sorted(attrs & typing_extensions_all)
mypy_extensions_added[v] = attrs & mypy_extensions_all
typing_extensions_added[v] = attrs & typing_extensions_all
for v, attrs in CUSTOM_TYPING_EXT_SYMBOLS.items():
typing_extensions_added[v] |= attrs

print(f'# GENERATED VIA {os.path.basename(sys.argv[0])}')
print('# Using:')
Expand Down

0 comments on commit c6d5e6c

Please sign in to comment.