Commit Graph
39 Commits
Author SHA1 Message Date
Alex Waygood d8c6e237e7 [ty] Split mismatched-type-name by type constructor (#29957)
## Summary

This PR splits `mismatched-type-name` into eight independently
configurable warnings for `NewType`, functional enums, `TypeVar`,
`ParamSpec`, `TypeVarTuple`, `namedtuple`/`NamedTuple`, `TypedDict`, and
`TypeAliasType`.

~~This PR is actually quite a bit less breaking for users than other
rule splits we've done over the last few days. Because
`mismatched-type-name` is no longer the canonical name for _any_ rule on
this branch, we're able to make it a deprecated alias for _all_ the new
rules, meaning that it will continue to work on the CLI, in ty
configuration files, and ty suppression comments (we'll just emit a
warning diagnostic telling users to upgrade their configuration
file/suppression comment).~~

~~A more specific rule takes precedence over the deprecated name if both
are given within the same configuration table. On the command line, the
last matching flag wins (e.g. `--error=mismatched-enum-name
--ignore=mismatched-type-name` will suppress diagnostics for mismatched
enum names).~~

(After review from @MichaReiser, I backed out these changes. It's now a
somewhat breaking change, because the rule is now marked as "removed"
rather than "deprecated".)

As well as splitting the rule into eight separate rules, it also cleans
up our error recovery for several of these type factories. For example,
in the following snippet, we fail to recognize `T` as a `TypeVar` due to
bad error recovery from the non-literal name. But we don't really need a
nonliteral name in this case to determine that it's a validly
constructed TypeVar. It's probably a mistake, but there's no _soundness_
issue here:

```py
from typing import TypeVar

def f(x: str):
    # main emits an error: `invalid-typevar`
    # this PR emits a warning: `mismatched-typevar-name`
    T = TypeVar(x)

    def g(x: T) -> T:
        return x
    
    reveal_type(g(42))  # main: Todo. This PR: `Literal[42]`.
```

## Motivation

The main motivation here is that having a namedtuple or enum with the
"wrong name", for example, just seems much less "obviously wrong" than
having a typevar or paramspec with the "wrong name". The [ecosystem
report](https://github.com/astral-sh/ruff/pull/29957#issuecomment-6603964612)
is evidence of this. namedtuples and enums define runtime classes that
can be used in runtime contexts and instantiated; these constructs
aren't _just_ for type checkers.
TypeVars/ParamSpecs/TypeVarTuples/NewTypes/TypedDicts are all _only_ for
type checkers and there's no plausible reason why you'd want to give the
type a different name to the variable it's assigned to.

`mismatched-namedtuple-name` and `mismatched-enum-name` are therefore
now disabled by default, matching the default severity we already
settled on for `mistmatched-sentinel-name` in
https://github.com/astral-sh/ruff/pull/27206. `mismatched-typevar-name`,
`mismatched-paramspec-name`, `mismatched-typevartuple-name`,
`mismatched-enum-name`, `mismatched-namedtuple-name` and
`mismatched-newtype-name` all remain enabled by default with `warning`
severity.
2026-11-20 12:41:39 +00:00
Micha ReiserandAlex Waygood 0222c19dd8 Share ty configuration for Ruff’s standalone scripts (#30463)
## Summary

Use a shared ty override for Ruff’s standalone scripts now that PEP 723
scripts can inherit project settings. Fix the diagnostics in the two PR
automation scripts so they can use the same checks.

## Notes to reviewers

All 28 standalone scripts passed ty with locked dependencies, and the
changed-file hooks and mocked workflow checks passed.

---------

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2026-11-16 17:35:08 +00:00
Alex WaygoodandMicha Reiser 22e77392fb [ty] Split invalid-return-type into 3 focussed rules; rename empty-body and unsound-return (#29947)
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-11-11 16:59:57 +00:00
Micha Reiser eeba58f5c2 [ty] Warn by default for a single overload (#30214)
## Summary

Today, `single-overload` defaults to severity error. I suggest we
downgrade the rule to warning. While this is most likely unintentional
and something the typing spec explicitly calls out type checker should
report. I don't think it should be an error:

* There's no runtime error, it does not lead to downstream errors (e.g.
because a value stores now a string instead of an int).
* It can be used to expose a narrower API than what the implementation
supports. TypeScript's compiler does not warn about a single-overlaod.
And the following is a valid use case in TypeScript, that should also
work in Python.

The use case is from VueJS:

```ts
// This component declares only the prop "foo".
useModel(props, "foo") // OK
useModel(props, "bar") // Type error: there is no "bar" prop
```

The `useModel` overload requires name (the second argument) to match
`Literal["foo"]`. So the second call is disallowed.


However, the `name` argument of the `useModel` implementation is
intentionally annotated as `name: string`, so that the implementation
can show a debug-only warning that the prop `bar` is disallowed, without
TypeScript raising any narrowing or dead code warnings.


This is certainly an advanced feature. But it suggests that there are
valid use cases for having a single overload, and that disqualifies it
from being a correctness rule.

Changing the severity doesn't change whether ty fails on CI, unless the
user explicitly opted in to existing with 0 when there are only warning
diagnostics.

## Test Plan

Verified the default severity in the CLI and ran the overload and typing
conformance tests.
2026-11-11 16:24:49 +01:00
David Peter c64734bcea [ty] Enable return type inference in scripts and tools (#29439)
## Summary

Enable return type inference for all 24 standalone Python scripts
checked by ty in CI, along with `py-fuzzer` and `ty_benchmark`.
2026-10-16 12:41:30 +00:00
Zanie Blueandzaniebot c79af46103 Require hashes for build dependencies (#29336)
Enable `require-build-hashes` for the Python projects and inline scripts
so isolated builds require declared hashes for their build dependencies.
Pin the build backends and their dependencies with hashes.

---------

Co-authored-by: zaniebot <242828183+zaniebot@users.noreply.github.com>
2026-10-15 20:15:23 +01:00
David Peter de9071547c [ty] Enable None-union truthiness checks in Python scripts and tools (#28911)
## Summary

Enable `truthiness-test-of-none-union` in the standalone scripts' inline
configuration and in the `py-fuzzer` and `ty_benchmark` projects,
following #28889.
2026-09-28 11:47:03 +00:00
Alex Waygood cbb1cdc35e [ty] Dogfood more ty rules on our own scripts (#28387) 2026-09-07 10:04:25 +01:00
Rob Hand 92bb198015 [ty] Align conformance.py counts with upstream (#28132)
## Summary
The upstream https://github.com/python/typing generated html now has a
breakdown of total test cases passing for each typechecker.

These total don't match what the `conformance.py` emits in it's summary:
https://github.com/astral-sh/ruff/pull/28001#issuecomment-5410032559

> The number of fully passing files held steady at 112/136. 

versus 

<img width="235" height="132" alt="image"
src="https://github.com/user-attachments/assets/6af76b8d-9002-4b00-ac87-b55afb0c15f8"
/>

This is because the summary is generated from `TestCase` which track
`ty` diagnostics, if there are no diagnostics because the relevant test
is designed to emit none they are not included in the count.

So instead we also use the full `test_files` to fill in the missing
stats.

NOTE: I have also bumped `CONFORMANCE_SUITE_COMMIT` which brings in some
recent spec related changes (not just version bumps)
 

## Test Plan

`CONFORMANCE_SUITE_COMMIT=cf943ccbea5596ef969eec6e2260097ff697b7ba uv
run --no-project --python 3.12 scripts/conformance.py --old-ty uvx
ty@0.0.75 --new-ty uvx ty@0.0.75 --tests-path ~/dev/typing/conformance`

```md
## [Typing conformance results](https://github.com/python/typing/blob/cf943ccbea5596ef969eec6e2260097ff697b7ba/conformance/)

### No changes detected ✅

<details>
<summary>Current numbers</summary>

<br>
The percentage of diagnostics emitted that were expected errors held steady at <b>97.79%</b>. The percentage of expected errors that received a diagnostic held steady at <b>94.41%</b>. The number of fully passing files held steady at <b>121/145</b>.

</details>
```
2026-08-28 17:20:42 -07:00
Alex Waygood b33f21debe [ty] Dogfood standalone scripts with inline configuration (#27885) 2026-08-19 15:55:30 +01:00
zaniebotandzaniebot 6dd1fa88eb Require wheels for Python tooling dependencies (#27861)
Python tooling can currently build third-party source distributions. Set
`no-build` in the maintained Python projects and PEP 723 scripts,
retaining named exceptions for Git-pinned tooling and the local
ecosystem runner. Pin the isolated build dependencies for those
exceptions, including their transitive requirements.

Follows merged astral-sh/ruff#27856, which gives the legacy `ruff-lsp`
suite a dedicated, locked Python 3.12 test environment.

Follow-up to astral-sh/ruff#27843. Replaces astral-sh/ruff#27840, which
closed when the merged docs branch was deleted.

Co-authored-by: zaniebot <242828183+zaniebot@users.noreply.github.com>
2026-08-19 14:31:32 +00:00
William WoodruffandAlex Waygood 7ed8d1fbdd Manage docs build with a dep group (#27843)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Signed-off-by: William Woodruff <william@yossarian.net>
2026-08-18 20:56:20 +00:00
William Woodruff 66418ef0d4 Use pinned forms for uvx (#27798)
Signed-off-by: William Woodruff <william@yossarian.net>
2026-08-16 16:26:55 -04:00
Charlie Marsh d1dda84a42 [ty] Reject undeclared protocol instance attributes (#26336)
## Summary

Protocol instance attributes must be declared in the protocol class body
or inherited from a superclass. We previously accepted attributes
introduced only by assignments to `self` inside protocol methods, even
though those attributes are not part of the protocol interface.

This emits `ambiguous-protocol-member` for ordinary, annotated,
annotation-only, and augmented assignments to undeclared protocol
instance attributes:

```python
class Template(Protocol):
    name: str

    def method(self) -> None:
        self.temp: list[int] = []  # warning: undeclared protocol member
```

Closes https://github.com/astral-sh/ty/issues/3758.
2026-06-24 19:07:51 +00:00
Charlie Marsh 4e54b8b771 [ty] Recognize exhaustive matches over finite tuples (#26132)
## Summary

Prior to this change, we could recognize exhaustive match statements
when a fixed tuple's finite combinations were written as an explicit
union, but not when those alternatives were hidden inside the tuple
element types:

```python
def f(b_1: bool, b_2: bool) -> int:
    match (b_1, b_2):
        case (True, True):
            return 0
        case (False, _):
            return 1
        case (_, False):
            return 2
```

We now share the existing bounded type-expansion logic used during
overload resolution and use the expanded union when computing match
reachability. For `tuple[bool, bool]`, this lets each preceding case
subtract the combinations it covers until no alternatives remain.

The expanded type is used only for reachability, so subject and capture
inference remain unchanged. Tuple products remain capped at 64
alternatives; larger products keep the existing conservative behavior.
This also resolves the existing `tuple[int | str]` sequence-pattern
exhaustiveness TODO.

Closes https://github.com/astral-sh/ty/issues/3807.
2026-06-22 20:51:07 -04:00
Charlie Marsh 35228e5a84 [ty] Respect commented-out lines in the conformance test suite (#24932)
## Summary

We were treating this line from the conformance tests as a false
negative:


https://github.com/python/typing/blob/5a701a037b5243df1f39622b642893d865f06205/conformance/tests/generics_syntax_infer_variance.py#L72
2026-04-29 12:56:26 -04:00
Charlie Marsh a1bd94d0f6 [ty] Check inherited NamedTuple field conflicts (#24542)
## Summary

Per the spec:

> A named tuple class can be subclassed, but any fields added by the
subclass are not considered part of the named tuple type. Type checkers
should enforce that these newly-added fields do not conflict with the
named tuple fields in the base class.

The spec then provides this example:

```python
class Point(NamedTuple):
    x: int
    y: int
    units: str = "meters"

class PointWithName(Point):
    name: str  # OK
    x: int  # Type error (invalid override of named tuple field)
```

We take a slightly more expansive view here, rejecting any attributes
(with or without an annotation, with or without a default) and
properties in a `NamedTuple` subclass. This seems to match Pyright and
Pyrefly, though Mypy doesn't flag these.

Shadowing an attribute in this way leads to odd behavior that is
probably never what you want, e.g.:

```python
from typing import NamedTuple

class A(NamedTuple):
    x: int

class B(A):
    x: int = 42

b = B(1)

print(b.x)  # 42
print(b[0])  # 1
print(repr(b))  # B(x=1)

b.x = 99

print(b.x)  # 99
print(b[0])  # 1
print(repr(b))  # B(x=1)
```
2026-04-14 11:30:54 -04:00
Charlie Marsh 08b0218b3e [ty] Improve consistency of pedantic lints complaining about badly named types (#24575)
## Summary

We now use the same error code for all of these, with the same range,
and it's a warning by default:
```python
N = NamedTuple("O", [])
TD = TypedDict("TE", {})
T = TypeVar("U")
P = ParamSpec("Q")
NT = NewType("N", int)
```

It also implements more graceful recovery for `TypeVar`,
`TypeAliasType`, `ParamSpec`, and `NewType`.

Closes https://github.com/astral-sh/ty/issues/3255.
2026-04-12 13:42:16 -04:00
Alex Waygood 5cdd2fe435 More minor improvements to conformance.py (#23792) 2026-03-07 17:54:21 +00:00
Alex Waygood c6418d2fee conformance.py: stop escaping | characters (#23758) 2026-03-06 11:01:27 +00:00
Alex Waygood 74b0553972 conformance.py: Collapse the summary paragraph when nothing changed (#23745) 2026-03-05 20:02:04 +00:00
Will DukeandAlex Waygood 69c23cc5a3 [ty] Render all changed diagnostics in conformance.py (#23613)
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2026-03-05 19:20:22 +00:00
Alex Waygood cd8e8d535f [ty] Add invalid-enum-member-annotation lint rule (#23648)
## Summary

This PR fixes the last remaining conformance failure on
`enums_members.py` in the conformance suite.

Implements a new lint rule `invalid-enum-member-annotation` that detects
type annotations on enum members. According to the typing spec, enum
members should not have explicit type annotations, as the actual runtime
type is the enum class itself, not the annotated type.

The rule:
- Flags annotated enum members (e.g., `DOG: int = 2` in an `Enum` class)
- Allows bare `Final` annotations (which don't specify a type)
- Excludes dunder names, private names, and special sunder names like
`_value_` and `_ignore_`
- Excludes pure declarations without values (non-members)

## Test Plan

mdtests
2026-03-02 17:31:36 +00:00
David Peter 9719e17c25 [ty] Diagnostic when combining Final and ClassVar (#23365)
## Summary

This PR implements the following paragraph in the typing spec:

> Type checkers should infer a final attribute that is initialized in a
class body as being a class variable, except in the case of
[Dataclasses](https://typing.python.org/en/latest/spec/dataclasses.html),
where `x: Final[int] = 3` creates a dataclass field and instance-level
final attribute `x` with default value `3`; `x: ClassVar[Final[int]] =
3` is necessary to create a final class variable with value `3`. In
non-dataclasses, combining `ClassVar` and `Final` is redundant, and type
checkers may choose to warn or error on the redundancy.
>
>
https://typing.python.org/en/latest/spec/qualifiers.html#semantics-and-examples

## Test Plan

New Markdown tests
2026-02-20 19:09:01 +01:00
renovate[bot] b243865f68 Update prek dependencies (#23320) 2026-02-16 01:48:12 +00:00
Alex Waygood 8b7db2d35a [ty] Pass --error=deprecated when running conformance.py (#23304)
## Summary

This change bumps the recall score reported by the script from 74.33% to
74.82%
2026-02-15 23:51:43 +00:00
Douglas CreagerandAlex Waygood dc65d12668 [ty] Infer typevar specializations for implicit generic protocols (#21902)
We now infer specializations that involve generic protocols. This
includes recursing into the methods of the protocol, matching up the
signatures of the class's methods with the signatures of the protocol,
and adding bindings for any typevars that appear in the protocol
signatures.

This required several performance updates, which were pulled out and
merged as separate PRs. There is still a performance and memory hit, but
I think a reasonable one, given the new functionality that this opens
up.

There are still some ecosystem false positives that relate to how this
interacts with overload resolution, but I plan to tackle that in
follow-on PRs.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2026-02-11 16:09:34 -05:00
Will Duke 0d1322d0b1 [ty] fix(conformance): Add tests directory as extra search path (#23048)
## Summary

The conformance test suite has some underscore-prefixed helper files
from which some of the test files import items. This change adds the
tests/ directory as a search path so that these imports no longer raise
spurious unresolved-import diagnostics.

## Test Plan

Running `scripts/conformance.py` after this change removes any
unresolved-import diagnostics and improves the number of true positives.


# Command

```
uv run --no-project --python 3.12 scripts/conformance.py --tests-path ../typing/conformance/ --old-ty uvx ty@0.0.6 --output scripts/test.md
```

## Before

### Summary

| Metric | Old | New | Diff | Outcome |
|--------|-----|-----|------|---------|
| True Positives  | 686 | 747 | +61 | ⏫ (✅) |
| False Positives | 244 | 201 | -43 | ⏬ (✅) |
| False Negatives | 387 | 332 | -55 | ⏬ (✅) |
| Total Diagnostics | 930 | 948 | +18 | ⏫ |
| Precision | 73.76% | 78.80% | +5.03% | ⏫ (✅) |
| Recall | 63.93% | 69.23% | +5.30% | ⏫ (✅) |

## After

### Summary

| Metric | Old | New | Diff | Outcome |
|--------|-----|-----|------|---------|
| True Positives  | 691 | 752 | +61 | ⏫ (✅) |
| False Positives | 234 | 191 | -43 | ⏬ (✅) |
| False Negatives | 383 | 328 | -55 | ⏬ (✅) |
| Total Diagnostics | 925 | 943 | +18 | ⏫ |
| Precision | 74.70% | 79.75% | +5.04% | ⏫ (✅) |
| Recall | 64.34% | 69.63% | +5.29% | ⏫ (✅) |
2026-02-03 09:20:37 +01:00
Alex Waygood 6f1e6cb37f [ty] Emit a diagnostic on incorrect applications of the legacy convention for specifying positional-only parameters (#22943) 2026-01-31 10:14:00 +00:00
Alex Waygood fcbf6f1cc9 [ty] Add a new assert-type-unspellable-subtype diagnostic (#22815)
Fixes https://github.com/astral-sh/ty/issues/1762. If a type assertion
fails, but the actual type is an unspellable subtype of the asserted
type, we now emit `assert-type-unspellable-subtype` rather than
`type-assertion-failure`. We also ignore this error in
`scripts/conformance.py`. This results in 17 fewer false positives on
the conformance suite, and it's also something that our users have asked
for.
2026-01-23 21:16:16 +00:00
Will DukeandMicha Reiser 58bffa465c [ty] Handle tagged errors in conformance (#22746)
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-01-23 14:44:28 +00:00
Alex Waygood 7b534a9b89 scripts/conformance.py: fix collection of diagnostics (#22671) 2026-01-18 11:52:38 +00:00
Alex Waygood ca57b2595e conformance.py: Only use the "No changes detected ✅" summary if no diagnostics were added or removed (#22649) 2026-01-17 13:30:45 +00:00
Will Duke dfd236c028 [ty] Handle optional errors in conformance workflow (#22647) 2026-01-17 12:47:45 +00:00
Alex Waygood 3c323559ed More fixes to scripts/conformance.py (#22616) 2026-01-16 12:47:39 +00:00
Alex Waygood aa9f1b27fc Further improvements to typing conformance script (#22596) 2026-01-15 15:49:02 +00:00
9c67b2acd9 [ty] Try fixing conformance workflow (#22593)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-01-15 12:38:13 +00:00
Micha Reiser c3ba638ef5 [ty] Better handling for neutral conformence test changes (#22585) 2026-01-15 08:53:11 +01:00
Will Duke 4ebf10cf1b [ty] Add a conformance script to compare ty diagnostics with expected errors (#22231) 2026-01-13 22:19:41 +00:00