Skip to content

Type Parameter Modernization - #202

Draft
FrancescoPezzella wants to merge 78 commits into
mainfrom
typing-recipes-2
Draft

FrancescoPezzella wants to merge 78 commits into
mainfrom
typing-recipes-2

Conversation

@FrancescoPezzella

@FrancescoPezzella FrancescoPezzella commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

This PR is about modernizing Type Parameters.

@FrancescoPezzella

FrancescoPezzella commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

Goal of this new PR:

  • Rebase Typing Recipes with Python Scanner which is rebased with Main ( i need its changed ).

In this way, I have managed to get rid of all the unnecessary clutter in Git History and also follow the workflow I saw other PRs follow.

@FrancescoPezzella
FrancescoPezzella changed the base branch from main to python-scanner September 21, 2026 12:52
@FrancescoPezzella
FrancescoPezzella added this pull request to stack #203 September 21, 2026 12:52
@FrancescoPezzella

Copy link
Copy Markdown
Collaborator Author

I am gonna try out this whole stacked thing, so my changes dont get blocked by some feature I could use.

@FrancescoPezzella FrancescoPezzella self-assigned this Sep 21, 2026
@FrancescoPezzella FrancescoPezzella added the enhancement New feature or request label Sep 21, 2026
Base automatically changed from python-scanner to main September 23, 2026 11:42
@FrancescoPezzella
FrancescoPezzella force-pushed the typing-recipes-2 branch 3 times, most recently from eaeef61 to f5168f2 Compare September 30, 2026 11:56
…orrectly split - adjusted the files to correctly fit the new convention. Added new hypothesis testing to test_type_var_check_properties.py that forces at least 1 case TypeVar.
…le usage in a single pass: localizes cross-file imports, converts every declared type parameter to PEP 695 syntax regardless of scope, and cleans up declarations left orphaned by ruff or manual conversion. Added the Recipes and Type parameter scope documentation pages, plus tests covering all three phases.
… collapsing isinstance narrowing to Never) by annotating it as object | None - verified with zero new pyright errors across src/ and no test regressions. Added the python-ast-known-limitations.md page documenting the remaining known gaps in the Python AST/RST layer found while building TypeVarCheck, and marked it done in docs/TODO.
…agraph and list-item lines to the project's 140-char limit, moved the anchor after the top-level heading in python-ast-known-limitations.md, and fixed a nested-list indent in core.md. No content changes, only wrapping/formatting.
…runs on 3.12+ targets, verified live against sqlalchemy), fixed two real bugs found in that testing (missing Or/MatMult in KIND_MAP, and the shared rewrite pipeline double-indenting multi-line docstrings), and cleaned up overly verbose comments and redundant tests.
…en an ast list field

holds a bare None (e.g. a keyword-only arg with no default in ast.arguments.kw_defaults)
…odel and safety analysis, shared with TypeVarTupleCheck instead of importing from another recipe's file) and utils/unparse_utils.py (the shared ast.unparse() docstring-indent workaround) out of type_var_check.py, and added find_rst_node/remove_import_alias as generic helpers on the PythonRefactoring base class so future recipes get them for free. type_var_check.py itself shrinks to pipeline orchestration only. Renamed five domain functions that were named as module-private (leading underscore) despite being genuinely used cross-file, matching pyright strict's reportPrivateUsage/reportUnusedFunction.

Split test_type_var_check.py by phase into test_type_var_check_convert.py, test_type_var_check_localize.py, test_type_var_check_orphaned.py, plus a shared conftest.py fixture replacing three near-duplicate private test helpers, and added direct tests for the two new base-class methods.

Reworded every docstring across the five touched files to satisfy ruff's full intended ruleset (E, F, W, I, N, B, D, UP, TD), not just the project's current E-only baseline - verified with pyright strict via a temporary per-file marker (never committed). Updated recipes.md, typevar-modernization.md and python-ast-known-limitations.md to match.
…ain.py that were actually ours to fix: added type_param_name() to narrow ast.type_param to its concrete subclasses (TypeVar/ParamSpec/TypeVarTuple), which lack a shared .name in the typeshed stubs, and removed 3 redundant casts now that PythonRstNode.node is already typed as ast.AST. Verified with ruff's full ruleset, the project's real pyright/ruff settings, and the full test suite.
…riter.py), work around it in TypeVarCheck, and document the fallout.

_RewriteActions.apply() now raises instead of silently concatenating two edits queued against overlapping source ranges, matching the pre-existing "Error cases" spec in features/rewrite-semantics.feature and un-xfailing test_replacing_same_node_twice_always_errors. TypeVarCheck avoids ever triggering it: PythonRefactoring.remove_import_alias() batches multiple names sharing one import into a single edit, and convert_declared_typevars does one replace() per function instead of one per type param. Added a regression test covering two type params sharing one import. The same fix also revealed a pre-existing instance of the bug in Taut2Pyunit, marked xfail(strict=True) and left unfixed. Documented as item 5 in python-ast-known-limitations.md.
…riter.py), work around it in TypeVarCheck, and document the fallout.
Resolves from-imports across the whole target codebase (not just __all__) before
localizing/removing a TypeVar declaration, via the new import_resolution.py utility.
Utilize the new PythonScanner function
…ible to bugs and unintended effects. User must now specify the Python version for which they would like to convert their project.

This removes a lot of code, and overall simplifies the tool.

This is a choice to not go overboard with implementations and not add things that already exist or can be solved in an easier way.
… default), this makes the test not replicate the real application (where UTF-8 is not always forced, yet)
…is gives the user a bit more control over the tool, allowing them to use what they wish, without being overly confusing.
@FrancescoPezzella FrancescoPezzella changed the title Typing recipes 2 Type Parameter Modernization Oct 2, 2026
@FrancescoPezzella

Copy link
Copy Markdown
Collaborator Author

Names such as Type Hints and Type Var were rejected, such as Type Hints being too broad and covering list[int] etc which other tools such as Ruff already do, while Type Var being imprecise and not covering all the changes made here such as ParamSpec and TypeVarTuple (which are not technically Type Vars).

I will from now on be referring to this as Type Parameter Modernization.

…ng time; EXCLUDED_DIRS was ignoring any path with an excluded keyword, even if it was outside of project, for example:

C:/venv/project_name/file_name

This would cause every file in the project to be ignored, which is not intended behavior

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant