Skip to content

Commit

Permalink
combine the typing rewrite info
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Jul 19, 2022
1 parent c6d5e6c commit cc178f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
12 changes: 4 additions & 8 deletions reorder_python_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,8 @@ def _fix_file(
# GENERATED VIA generate-typing-rewrite-info
# Using:
# flake8-typing-imports==1.12.0
# mypy_extensions==0.4.3
# typing_extensions==4.3.0
REPLACES[(3, 7)].update((
'mypy_extensions=typing:NoReturn',
))
REPLACES[(3, 8)].update((
'mypy_extensions=typing:TypedDict',
))
# mypy-extensions==0.4.3
# typing-extensions==4.3.0
REPLACES[(3, 6)].update((
'typing_extensions=typing:AsyncIterable',
'typing_extensions=typing:AsyncIterator',
Expand All @@ -443,6 +437,7 @@ def _fix_file(
'typing_extensions=typing:get_type_hints',
))
REPLACES[(3, 7)].update((
'mypy_extensions=typing:NoReturn',
'typing_extensions=typing:AsyncContextManager',
'typing_extensions=typing:AsyncGenerator',
'typing_extensions=typing:ChainMap',
Expand All @@ -451,6 +446,7 @@ def _fix_file(
'typing_extensions=typing:NoReturn',
))
REPLACES[(3, 8)].update((
'mypy_extensions=typing:TypedDict',
'typing_extensions=typing:Final',
'typing_extensions=typing:Literal',
'typing_extensions=typing:OrderedDict',
Expand Down
41 changes: 19 additions & 22 deletions testing/generate-typing-rewrite-info
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
from __future__ import annotations

import collections
import os
import sys
from collections import defaultdict

import flake8_typing_imports
import mypy_extensions
Expand Down Expand Up @@ -50,53 +50,50 @@ def main() -> int:
for attr in attrs:
min_contiguous_versions.setdefault(attr, v)

symbols: dict[flake8_typing_imports.Version, set[str]] = defaultdict(set)
symbols = collections.defaultdict(set)
for a, v in min_contiguous_versions.items():
symbols[v].add(a)

# --pyXX-plus assumes the min --pyXX so group symbols by their
# rounded up major version
symbols_rounded_up: dict[tuple[int, int], set[str]] = defaultdict(set)
symbols_rounded_up: dict[tuple[int, int], set[str]]
symbols_rounded_up = collections.defaultdict(set)
for v, attrs in sorted(symbols.items()):
symbols_rounded_up[v.major, v.minor + int(v.patch != 0)] |= attrs

# combine 3.5 and 3.6 because this lib is 3.7+
symbols_rounded_up[(3, 6)] |= symbols_rounded_up.pop((3, 5))

deltas: dict[tuple[int, int], set[str]] = defaultdict(set)
deltas = collections.defaultdict(set)
prev: set[str] = set()
for v, attrs in sorted(symbols_rounded_up.items()):
deltas[v] = attrs - prev
prev = attrs

mypy_extensions_added: dict[tuple[int, int], set[str]] = {}
typing_extensions_added: dict[tuple[int, int], set[str]] = defaultdict(set)
replaces: dict[tuple[int, int], set[str]] = collections.defaultdict(set)
for v, attrs in deltas.items():
mypy_extensions_added[v] = attrs & mypy_extensions_all
typing_extensions_added[v] = attrs & typing_extensions_all
replaces[v] |= {
f'mypy_extensions=typing:{s}'
for s in attrs & mypy_extensions_all
} | {
f'typing_extensions=typing:{s}'
for s in attrs & typing_extensions_all
}
for v, attrs in CUSTOM_TYPING_EXT_SYMBOLS.items():
typing_extensions_added[v] |= attrs
replaces[v] |= {f'typing_extensions=typing:{s}' for s in attrs}

print(f'# GENERATED VIA {os.path.basename(sys.argv[0])}')
print('# Using:')
print(f'# flake8-typing-imports=={flake8_typing_imports_version}')
print(f'# mypy_extensions=={mypy_extensions_version}')
print(f'# typing_extensions=={typing_extensions_version}')
print(f'# mypy-extensions=={mypy_extensions_version}')
print(f'# typing-extensions=={typing_extensions_version}')

for k, v in sorted(mypy_extensions_added.items()):
for k, v in sorted(replaces.items()):
if not v:
continue
print(f'REPLACES[{k}].update((')
for symbol in sorted(v):
print(f" 'mypy_extensions=typing:{symbol}',")
print('))')

for k, v in sorted(typing_extensions_added.items()):
if not v:
continue
print(f'REPLACES[{k}].update((')
for symbol in sorted(v):
print(f" 'typing_extensions=typing:{symbol}',")
for replace in sorted(v):
print(f' {replace!r},')
print('))')

print('# END GENERATED')
Expand Down

0 comments on commit cc178f2

Please sign in to comment.