Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Nov 14, 2025

Summary

Infer the first argument type inside Annotated[type, …] as a type expression. This allows us to support stringified annotations inside Annotated.

Ecosystem

  • The removed diagnostic on prefect shows that we now understand the State.data type annotation in src/prefect/client/schemas/objects.py:230, which uses a stringified annotation in Annoated. The other diagnostics are downstream changes that result from this, it seems to be a commonly used data type.
  • artigraph does something like Annotated[cast(Any, field_info.annotation), *field_info.metadata] which I'm not sure we need to allow? It's unfortunate since this is probably supported at runtime, but it seems reasonable that they need to add a # type: ignore for that.
  • pydantic uses something like Annotated[(self.annotation, *self.metadata)] but adds a # type: ignore

Test Plan

New Markdown test

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Nov 14, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

mypy_primer results

Changes were detected when running on open source projects
beartype (https://github.com/beartype/beartype)
+ beartype/bite/collection/infercollectionsabc.py:200:26: error[invalid-type-form] Variable of type `object` is not allowed in a type expression
- Found 498 diagnostics
+ Found 499 diagnostics

artigraph (https://github.com/artigraph/artigraph)
+ src/arti/internal/models.py:101:31: error[invalid-type-form] Function calls are not allowed in type expressions
- Found 174 diagnostics
+ Found 175 diagnostics

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/client/schemas/objects.py:230:13: error[invalid-type-form] Variable of type `Literal["ResultRecordMetadata"]` is not allowed in a type expression
+ src/prefect/states.py:515:22: error[not-iterable] Object of type `~None & ~BaseException & ~str & ~Top[State[Any]]` is not iterable
+ src/prefect/states.py:616:22: error[not-iterable] Object of type `~None & ~BaseException & ~str & ~Top[State[Any]]` is not iterable
+ src/prefect/testing/utilities.py:241:9: error[invalid-argument-type] Argument to bound method `aread` is incorrect: Expected `str`, found `str | None`
+ src/prefect/testing/utilities.py:258:50: error[invalid-argument-type] Argument to bound method `read_block_document` is incorrect: Expected `UUID`, found `UUID | None`
+ src/prefect/testing/utilities.py:269:50: error[invalid-argument-type] Argument to bound method `read_block_document` is incorrect: Expected `UUID`, found `UUID | None`
- Found 3233 diagnostics
+ Found 3237 diagnostics

pydantic (https://github.com/pydantic/pydantic)
- pydantic/fields.py:780:66: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- Found 4818 diagnostics
+ Found 4817 diagnostics

No memory usage changes detected ✅

@sharkdp sharkdp force-pushed the david/annotated-stringified branch from 8dcd2c8 to 37aaaca Compare November 14, 2025 11:39
@sharkdp sharkdp marked this pull request as ready for review November 14, 2025 11:54
Comment on lines +186 to +195
KnownInstanceType::Annotated(ty) => {
// Unions are not supported in this position, so we only need to support
// something like `class C(Annotated[Base, "metadata"]): ...`, which we
// can do by turning the instance type (`Base` in this example) back into
// a class.
let annotated_ty = ty.inner(db);
let instance_ty = annotated_ty.as_nominal_instance()?;

Some(Self::Class(instance_ty.class(db)))
}
Copy link
Member

Choose a reason for hiding this comment

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

hmmmm I feel like there's probably going to be some edge case someone will come up with at some point where this lossy conversion will come back to bite us... but I'm not sure what they are, and this seems fine for now 😆

@sharkdp sharkdp enabled auto-merge (squash) November 14, 2025 13:05
@sharkdp sharkdp merged commit e9a5337 into main Nov 14, 2025
40 checks passed
@sharkdp sharkdp deleted the david/annotated-stringified branch November 14, 2025 13:09
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)
  ...
dcreager added a commit that referenced this pull request Nov 14, 2025
* dcreager/deep-comparison: (64 commits)
  assuming
  SubtypingAssuming
  implies_subtype_of
  name tweak
  Apply suggestions from code review
  [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)
  ...
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.

3 participants