ruff/crates/ty_python_semantic/resources/mdtest
Douglas Creager aba0bd568e
[ty] Diagnostic for generic classes that reference typevars in enclosing scope (#20822)
Generic classes are not allowed to bind or reference a typevar from an
enclosing scope:

```py
def f[T](x: T, y: T) -> None:
    class Ok[S]: ...
    # error: [invalid-generic-class]
    class Bad1[T]: ...
    # error: [invalid-generic-class]
    class Bad2(Iterable[T]): ...

class C[T]:
    class Ok1[S]: ...
    # error: [invalid-generic-class]
    class Bad1[T]: ...
    # error: [invalid-generic-class]
    class Bad2(Iterable[T]): ...
```

It does not matter if the class uses PEP 695 or legacy syntax. It does
not matter if the enclosing scope is a generic class or function. The
generic class cannot even _reference_ an enclosing typevar in its base
class list.

This PR adds diagnostics for these cases.

In addition, the PR adds better fallback behavior for generic classes
that violate this rule: any enclosing typevars are not included in the
class's generic context. (That ensures that we don't inadvertently try
to infer specializations for those typevars in places where we
shouldn't.) The `dulwich` ecosystem project has [examples of
this](d912eaaffd/dulwich/config.py (L251))
that were causing new false positives on #20677.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2025-10-13 19:30:49 -04:00
..
annotations [ty] Do not bind self to non-positional parameters (#20850) 2025-10-13 20:44:27 +02:00
assignment [ty] bidirectional type inference using function return type annotations (#20528) 2025-10-11 00:38:35 +00:00
binary Update default and latest Python versions for 3.14 (#20725) 2025-10-07 12:23:11 -04:00
boolean
boundness_declaredness [ty] Reformulation of public symbol inference test suite (#20667) 2025-10-01 14:26:17 +02:00
call [ty] Treat Callables as bound-method descriptors in special cases (#20802) 2025-10-13 21:17:47 +02:00
class [ty] Fix false-positive diagnostics on super() calls (#20814) 2025-10-13 10:57:46 +00:00
comparison [ty] detect cycles in binary comparison inference (#20446) 2025-09-17 09:45:25 +02:00
comprehensions
conditional
dataclasses [ty] Treat functions, methods, and dynamic types as function-like Callables (#20842) 2025-10-13 15:21:55 +02:00
declaration
diagnostics [ty] defer inference of legacy TypeVar bound/constraints/defaults (#20598) 2025-10-09 21:08:37 +00:00
directives [ty] Use annotated parameters as type context (#20635) 2025-10-03 17:14:51 -04:00
doc
exception [ty] Filter out revealed-type and undefined-reveal diagnostics from mdtest snapshots (#20820) 2025-10-12 18:39:32 +00:00
expression [ty] Rename "possibly unbound" diagnostics to "possibly missing" (#20492) 2025-09-23 14:26:55 +00:00
function
generics [ty] Diagnostic for generic classes that reference typevars in enclosing scope (#20822) 2025-10-13 19:30:49 -04:00
ide_support [ty] Filter out revealed-type and undefined-reveal diagnostics from mdtest snapshots (#20820) 2025-10-12 18:39:32 +00:00
import [ty] No union with Unknown for module-global symbols (#20664) 2025-10-01 16:40:30 +02:00
literal [ty] Treat functions, methods, and dynamic types as function-like Callables (#20842) 2025-10-13 15:21:55 +02:00
loops [ty] Filter out revealed-type and undefined-reveal diagnostics from mdtest snapshots (#20820) 2025-10-12 18:39:32 +00:00
narrow [ty] Filter out revealed-type and undefined-reveal diagnostics from mdtest snapshots (#20820) 2025-10-12 18:39:32 +00:00
regression
scopes Update default and latest Python versions for 3.14 (#20725) 2025-10-07 12:23:11 -04:00
shadowing
snapshots [ty] Diagnostic for generic classes that reference typevars in enclosing scope (#20822) 2025-10-13 19:30:49 -04:00
stubs Update default and latest Python versions for 3.14 (#20725) 2025-10-07 12:23:11 -04:00
subscript [ty] Rename "possibly unbound" diagnostics to "possibly missing" (#20492) 2025-09-23 14:26:55 +00:00
suppressions
type_compendium [ty] Type-context aware literal promotion (#20776) 2025-10-09 16:53:53 -04:00
type_of
type_properties [ty] Treat functions, methods, and dynamic types as function-like Callables (#20842) 2025-10-13 15:21:55 +02:00
type_qualifiers [ty] Allow annotation expressions to be ast::Attribute nodes (#20413) 2025-09-15 12:06:48 +01:00
unary
with [ty] Use typing.Self for the first parameter of instance methods (#20517) 2025-09-29 21:08:08 +02:00
.mdformat.toml
async.md
attributes.md [ty] Prefer declared base class attribute over inferred attribute on subclass (#20764) 2025-10-13 09:28:57 +02:00
bidirectional.md [ty] bidirectional type inference using function return type annotations (#20528) 2025-10-11 00:38:35 +00:00
classes.md
cycle.md
decorators.md
del.md [ty] No union with Unknown for module-global symbols (#20664) 2025-10-01 16:40:30 +02:00
deprecated.md
descriptor_protocol.md [ty] Use typing.Self for the first parameter of instance methods (#20517) 2025-09-29 21:08:08 +02:00
enums.md [ty] Enums: allow multiple aliases to point to the same member (#20669) 2025-10-01 15:51:53 +02:00
exhaustiveness_checking.md [ty] Fix subtyping for dynamic specializations (#20592) 2025-09-26 15:05:03 +02:00
final.md
implicit_type_aliases.md [ty] no more diverging query cycles in type expressions (#20359) 2025-09-16 16:44:11 -07:00
instance_layout_conflict.md
intersection_types.md
invalid_syntax.md
known_constants.md
mdtest_config.md
mdtest_custom_typeshed.md
metaclass.md
mro.md [ty] Filter out revealed-type and undefined-reveal diagnostics from mdtest snapshots (#20820) 2025-10-12 18:39:32 +00:00
named_tuple.md [ty] Use typing.Self for the first parameter of instance methods (#20517) 2025-09-29 21:08:08 +02:00
overloads.md [ty] Improve diagnostics for bad @overload definitions (#20745) 2025-10-07 21:52:57 +00:00
pep695_type_aliases.md [ty] defer inference of legacy TypeVar bound/constraints/defaults (#20598) 2025-10-09 21:08:37 +00:00
properties.md [ty] "foo".startswith is not an instance of types.MethodWrapperType (#20317) 2025-09-10 11:14:26 +00:00
protocols.md [ty] Filter out revealed-type and undefined-reveal diagnostics from mdtest snapshots (#20820) 2025-10-12 18:39:32 +00:00
public_types.md [ty] Disambiguate classes that live in different modules but have the same fully qualified names (#20756) 2025-10-08 18:27:40 +01:00
statically_known_branches.md [ty] Rename "possibly unbound" diagnostics to "possibly missing" (#20492) 2025-09-23 14:26:55 +00:00
sys_platform.md
sys_version_info.md
t_strings.md
terminal_statements.md
ty_extensions.md [ty] Simplify and fix CallableTypeOf[..] implementation (#20797) 2025-10-10 12:04:37 +02:00
typed_dict.md [ty] bidirectional type inference using function return type annotations (#20528) 2025-10-11 00:38:35 +00:00
union_types.md [ty] Introduce TypeRelation::Redundancy (#20602) 2025-10-03 18:35:30 +01:00
unpacking.md [ty] Infer more precise types for collection literals (#20360) 2025-09-17 18:51:50 -04:00
unreachable.md [ty] Rename "possibly unbound" diagnostics to "possibly missing" (#20492) 2025-09-23 14:26:55 +00:00