Skip to content

Commit

Permalink
[cdd/shared/ast_utils.py] Resolve TODO from `deduplicate_sorted_impor…
Browse files Browse the repository at this point in the history
…ts` to also deduplicate individual `ImportFrom`s ; [cdd/__init__.py] Bump version
  • Loading branch information
SamuelMarks committed Feb 13, 2024
1 parent 2e80ab1 commit 64ebf80
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cdd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from logging import getLogger as get_logger

__author__ = "Samuel Marks" # type: str
__version__ = "0.0.99rc29" # type: str
__version__ = "0.0.99rc30" # type: str
__description__ = (
"Open API to/fro routes, models, and tests. "
"Convert between docstrings, classes, methods, argparse, pydantic, and SQLalchemy."
Expand Down
46 changes: 34 additions & 12 deletions cdd/shared/ast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2381,23 +2381,45 @@ def deduplicate_sorted_imports(module):
),
None,
)
name_seen = set()

module.body = (
module.body[:fst_import_idx]
+ [
# TODO: Infer `level` and deduplicate `names`
ImportFrom(
module=name,
names=sorted(
chain.from_iterable(map(attrgetter("names"), import_from_nodes)),
key=attrgetter("name"),
),
level=0, # import_from_nodes[0].level
identifier=None,
)
for name, import_from_nodes in groupby(
module.body[fst_import_idx:lst_import_idx], key=attrgetter("module")
import_from
for import_from in (
# TODO: Infer `level`
ImportFrom(
module=name,
names=list(
filter(
lambda _alias: (
lambda key: (
False
if key in name_seen
else (name_seen.add(key) or True)
)
)(
"<name={!r}, alias.name={!r}, alias.asname={!r}>".format(
name, _alias.name, _alias.asname
)
),
sorted(
chain.from_iterable(
map(attrgetter("names"), import_from_nodes)
),
key=attrgetter("name"),
),
)
),
level=0, # import_from_nodes[0].level
identifier=None,
)
for name, import_from_nodes in groupby(
module.body[fst_import_idx:lst_import_idx], key=attrgetter("module")
)
)
if import_from.names
]
+ module.body[lst_import_idx:]
)
Expand Down

0 comments on commit 64ebf80

Please sign in to comment.