[ty] Support type[…] and Type[…] in implicit type aliases#21421
Merged
[ty] Support type[…] and Type[…] in implicit type aliases#21421
type[…] and Type[…] in implicit type aliases#21421Conversation
bbfbd6e to
ba8c48e
Compare
Diagnostic diff on typing conformance testsChanges were detected when running ty on typing conformance tests--- old-output.txt 2025-11-13 15:34:17.020867210 +0000
+++ new-output.txt 2025-11-13 15:34:20.477879574 +0000
@@ -900,12 +900,11 @@
specialtypes_type.py:120:5: error[unresolved-attribute] Object of type `type` has no attribute `unknown`
specialtypes_type.py:127:5: error[type-assertion-failure] Argument does not have asserted type `ProUser`
specialtypes_type.py:137:5: error[type-assertion-failure] Argument does not have asserted type `type[Any]`
-specialtypes_type.py:138:5: error[type-assertion-failure] Argument does not have asserted type `type[Any]`
specialtypes_type.py:139:5: error[type-assertion-failure] Argument does not have asserted type `type[Any]`
-specialtypes_type.py:140:5: error[type-assertion-failure] Argument does not have asserted type `type[Any]`
specialtypes_type.py:143:1: error[unresolved-attribute] Object of type `typing.Type` has no attribute `unknown`
specialtypes_type.py:145:1: error[unresolved-attribute] Class `type` has no attribute `unknown`
-specialtypes_type.py:146:1: error[unresolved-attribute] Object of type `GenericAlias` has no attribute `unknown`
+specialtypes_type.py:152:16: error[invalid-type-form] `typing.TypeVar` is not a generic class
+specialtypes_type.py:156:16: error[invalid-type-form] `typing.TypeVar` is not a generic class
specialtypes_type.py:160:1: error[type-assertion-failure] Argument does not have asserted type `int`
specialtypes_type.py:161:1: error[type-assertion-failure] Argument does not have asserted type `int`
specialtypes_type.py:169:5: error[invalid-assignment] Object of type `type` is not assignable to `type[int]`
@@ -1001,5 +1000,5 @@
typeddicts_usage.py:28:17: error[missing-typed-dict-key] Missing required key 'name' in TypedDict `Movie` constructor
typeddicts_usage.py:28:18: error[invalid-key] Invalid key for TypedDict `Movie`: Unknown key "title"
typeddicts_usage.py:40:24: error[invalid-type-form] The special form `typing.TypedDict` is not allowed in type expressions. Did you mean to use a concrete TypedDict or `collections.abc.Mapping[str, object]` instead?
-Found 1003 diagnostics
+Found 1002 diagnostics
WARN A fatal error occurred while checking some files. Not all project files were analyzed. See the diagnostics list above for details.
|
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-type-form |
1 | 57 | 0 |
unused-ignore-comment |
0 | 10 | 0 |
unsupported-operator |
1 | 4 | 0 |
invalid-argument-type |
3 | 0 | 1 |
unknown-argument |
2 | 0 | 0 |
invalid-parameter-default |
1 | 0 | 0 |
invalid-return-type |
1 | 0 | 0 |
unresolved-attribute |
1 | 0 | 0 |
| Total | 10 | 71 | 1 |
ba8c48e to
77eb115
Compare
type[…] in implicit type aliasestype[…] and Type[…] in implicit type aliases
sharkdp
commented
Nov 13, 2025
| // doesn't work and isn't allowed. | ||
| | KnownInstanceType::NewType(_) => None, | ||
| KnownInstanceType::TypeGenericAlias(_) => { | ||
| Self::try_from_type(db, KnownClass::Type.to_class_literal(db), subclass) |
Contributor
Author
There was a problem hiding this comment.
This is mostly based on the TODO comment in mdtest/type_of/basic.md. Do we need to preserve more information here? I don't really understand what class Foo(type[int]): ... tries to achieve.
Member
There was a problem hiding this comment.
I don't really understand what
class Foo(type[int]): ...tries to achieve.
Nor do I. I think this is fine.
AlexWaygood
approved these changes
Nov 13, 2025
| if class.is_tuple(self.db()) { | ||
| return tuple_generic_alias(self.db(), self.infer_tuple_type_expression(slice)); | ||
| } else if class.is_known(self.db(), KnownClass::Type) { | ||
| let argument_ty = self.infer_type_expression(slice); |
Member
There was a problem hiding this comment.
this is fine for now, but note that it results in us being too permissive when parsing the argument to type[]. For example, on your branch:
from typing import Literal
X = type[Literal[42]] # no error here, but there should be?
def f(
x: type[Literal[42]], # error: [invalid-type-form]
y: X # no error here either...
):
reveal_type(x) # revealed: @Todo(unsupported nested subscript in type[X])
reveal_type(y) # revealed: `<class 'int'>`
Contributor
Author
There was a problem hiding this comment.
Thanks. I'll add a test with a TODO in my next PR.
dcreager
added a commit
that referenced
this pull request
Nov 14, 2025
* origin/main: (59 commits) [ty] Improve diagnostic range for `non-subscriptable` diagnostics (#21461) [ty] Improve literal promotion heuristics (#21439) [ty] Further improve details around which expressions should be deferred in stub files (#21456) [ty] Improve generic class constructor inference (#21442) [ty] Propagate type context through conditional expressions (#21443) [ty] Suppress completions when introducing names with `as` [ty] Add panic-by-default await methods to `TestServer` (#21451) [ty] name is parameter and global is a syntax error (#21312) [ty] Fixup a few details around version-specific dataclass features (#21453) [ty] Support attribute-expression `TYPE_CHECKING` conditionals (#21449) [ty] Support stringified annotations in value-position `Annotated` instances (#21447) [ty] Type inference for genererator expressions (#21437) [ty] Make `__getattr__` available for `ModuleType` instances (#21450) [ty] Increase default receive timeout in tests to 10s (#21448) [ty] Add synthetic members to completions on dataclasses (#21446) [ty] Support legacy `typing` special forms in implicit type aliases (#21433) Bump 0.14.5 (#21435) [ty] Support `type[…]` and `Type[…]` in implicit type aliases (#21421) [ty] Respect notebook cell boundaries when adding an auto import (#21322) Update PyCharm setup instructions (#21409) ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Support
type[…]in implicit type aliases, for example:part of astral-sh/ty#221
Typing conformance
Two new tests passing ✔️
-specialtypes_type.py:146:1: error[unresolved-attribute] Object of type `GenericAlias` has no attribute `unknown`An
TA4.unknownattribute on a PEP 613 alias (TA4: TypeAlias = type[Any]) is being accessed, and the conformance suite expects this to be an error. Since we currently use the inferred type for these type aliases (and possibly in the future as well), we treat this as a direct access of the attribute ontype[Any], which falls back to an access onAnyitself, which succeeds. 🔴New errors because we don't handle
T = TypeVar("T"); MyType = type[T]; MyType[T]yet. Support for this is being tracked in astral-sh/ty#221 🔴Ecosystem impact
Looks mostly good, a few known problems.
Test Plan
New Markdown tests