Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] Report redefined legacy TypeVars#25854

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/fix-redefined-typevar
Jun 12, 2026
Merged

[ty] Report redefined legacy TypeVars#25854
charliermarsh merged 1 commit into
mainfrom
charlie/fix-redefined-typevar

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 11, 2026

Copy link
Copy Markdown
Member

Summary

The invalid-legacy-type-variable rule already validates the assignment shape and declared name, but it did not report when a name had already been defined in the same scope:

from typing import TypeVar

T = TypeVar("T")
T = TypeVar("T")  # error: invalid-legacy-type-variable

The diagnostic points back to the preceding definition and considers earlier bindings and declarations that are reachable from the beginning of the owning scope.

The implementation reuses the existing per-place use-def snapshot and reachability predicates, so it only inspects definitions of the target name rather than scanning every definition in the scope.

Closes astral-sh/ty#3732.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 11, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 11, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 94.36%. The percentage of expected errors that received a diagnostic held steady at 88.82%. The number of fully passing files held steady at 93/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 11, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 11, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-legacy-type-variable 1 0 0
Total 1 0 0

Raw diff:

setuptools (https://github.com/pypa/setuptools)
+ setuptools/_vendor/jaraco/functools/__init__.py:677:1 error[invalid-legacy-type-variable] Cannot redefine `_T` as a type variable

Full report with detailed diff (timing results)

@charliermarsh charliermarsh force-pushed the charlie/fix-redefined-typevar branch 2 times, most recently from 65778d4 to 7234735 Compare June 11, 2026 13:43
@charliermarsh charliermarsh marked this pull request as ready for review June 11, 2026 13:45
@charliermarsh charliermarsh force-pushed the charlie/fix-redefined-typevar branch from 7234735 to 95f0739 Compare June 11, 2026 13:49
@AlexWaygood AlexWaygood removed their request for review June 11, 2026 13:50
Comment thread crates/ty_python_semantic/resources/mdtest/generics/legacy/callables.md Outdated
Comment thread crates/ty_python_semantic/resources/mdtest/generics/legacy/variance.md Outdated
Comment thread crates/ty_python_semantic/src/types/infer/builder/typevar.rs Outdated
Comment thread crates/ty_python_semantic/src/types/infer/builder.rs
@charliermarsh charliermarsh marked this pull request as draft June 11, 2026 22:15
@charliermarsh charliermarsh force-pushed the charlie/fix-redefined-typevar branch from 3ecad3d to c43d712 Compare June 11, 2026 22:19
@charliermarsh charliermarsh force-pushed the charlie/fix-redefined-typevar branch from c43d712 to 05b9c29 Compare June 11, 2026 22:39
@charliermarsh charliermarsh changed the base branch from main to charlie/avoid-typevar-redefinitions-in-mdtests June 11, 2026 22:39
@charliermarsh charliermarsh marked this pull request as ready for review June 11, 2026 22:42
@astral-sh-bot astral-sh-bot Bot requested review from dcreager and oconnor663 June 11, 2026 22:42
@charliermarsh

Copy link
Copy Markdown
Member Author

Ok this should be quite a bit clearer @dcreager -- I also moved the mdtest churn out to #25889.

@dcreager dcreager left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Comment thread crates/ty_python_semantic/src/types/infer/builder.rs
charliermarsh added a commit that referenced this pull request Jun 12, 2026
## Summary

An mdtest section is evaluated as a single Python module, even when its
examples are split across multiple fenced code blocks. Some existing
sections therefore define the same legacy `TypeVar` name more than once:

```python
T = TypeVar("T")
# ...
T = TypeVar("T")
```

#25854 will correctly diagnose the second definition, but these
duplicates are incidental to the tests. This separates self-contained
examples into distinct mdtest sections and gives the remaining type
variables distinct names where the examples depend on shared setup. The
expected types and diagnostics are unchanged.

This is the preparatory base for #25854 and doesn't change behavior.
Base automatically changed from charlie/avoid-typevar-redefinitions-in-mdtests to main June 12, 2026 01:52
@charliermarsh charliermarsh force-pushed the charlie/fix-redefined-typevar branch from 05b9c29 to 59a62d9 Compare June 12, 2026 01:53
@charliermarsh charliermarsh enabled auto-merge (squash) June 12, 2026 01:53
@charliermarsh charliermarsh merged commit 094025e into main Jun 12, 2026
58 checks passed
@charliermarsh charliermarsh deleted the charlie/fix-redefined-typevar branch June 12, 2026 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

invalid-legacy-type-variable - false negative on redefined TypeVar

3 participants