Skip to content

Conversation

sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Jun 3, 2025

Summary

Came across this while debugging some ecosystem changes in #18347. I think the meta-type of a typevar-annotated variable should be equal to type, not <class 'object'>.

Test Plan

New Markdown tests.

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Jun 3, 2025
Copy link
Contributor

github-actions bot commented Jun 3, 2025

mypy_primer results

Changes were detected when running on open source projects
beartype (https://github.com/beartype/beartype)
- error[invalid-return-type] beartype/_util/utilobjmake.py:194:12: Return type does not match returned value: expected `T`, found `object`
- Found 573 diagnostics
+ Found 572 diagnostics

pydantic (https://github.com/pydantic/pydantic)
+ warning[unused-ignore-comment] pydantic/v1/fields.py:1049:49: Unused blanket `type: ignore` directive
- Found 760 diagnostics
+ Found 761 diagnostics

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+ warning[unused-ignore-comment] src/hydra_zen/structured_configs/_implementations.py:1074:60: Unused blanket `type: ignore` directive
- error[invalid-return-type] src/hydra_zen/structured_configs/_implementations.py:1314:20: Return type does not match returned value: expected `_T`, found `object`

@sharkdp sharkdp force-pushed the david/typevar-meta-type branch from 2215b9a to 8a9dd17 Compare June 3, 2025 10:10
@sharkdp sharkdp added the bug Something isn't working label Jun 3, 2025
@sharkdp sharkdp marked this pull request as ready for review June 3, 2025 10:14
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Makes sense to me! The implicit upper bound of a TypeVar that does not have bounds or constraints is object, and the meta-type of object is type.

(tiny nit: I would spell it "meta-type" rather than "meta type")

@sharkdp sharkdp force-pushed the david/typevar-meta-type branch from 8a9dd17 to ee1d82d Compare June 3, 2025 11:43
@sharkdp
Copy link
Contributor Author

sharkdp commented Jun 3, 2025

Makes sense to me! The implicit upper bound of a TypeVar that does not have bounds or constraints is object.

Yes: That's also why I added that second test: to show that an unbounded T behaves the same way as T: object 👍

@sharkdp sharkdp changed the title [ty] Meta type of type variables should be type[..] [ty] Meta-type of type variables should be type[..] Jun 3, 2025
@sharkdp sharkdp merged commit 0986edf into main Jun 3, 2025
35 checks passed
@sharkdp sharkdp deleted the david/typevar-meta-type branch June 3, 2025 13:22
dcreager added a commit that referenced this pull request Jun 3, 2025
…aration

* origin/main:
  [ty] Infer `list[T]` when unpacking non-tuple type (#18438)
  [ty] Meta-type of type variables should be type[..] (#18439)
  [`pyupgrade`] Make fix unsafe if it deletes comments (`UP050`) (#18390)
  [`pyupgrade`] Make fix unsafe if it deletes comments (`UP004`) (#18393)
  [ty] Support using legacy typing aliases for generic classes in type annotations (#18404)
  Use ty's completions in playground (#18425)
  Update editor setup docs about Neovim and Vim (#18324)
  Update NPM Development dependencies (#18423)
  Infer `list[T]` for starred target in unpacking (#18401)
  [`refurb`] Mark `FURB180` fix unsafe when class has bases (#18149)
  [`fastapi`] Avoid false positive for class dependencies (`FAST003`) (#18271)
carljm added a commit to mtshiba/ruff that referenced this pull request Jun 4, 2025
* main:
  [ty] Add tests for empty list/tuple unpacking (astral-sh#18451)
  [ty] Argument type expansion for overload call evaluation (astral-sh#18382)
  [ty] Minor cleanup for `site-packages` discovery logic (astral-sh#18446)
  [ty] Add generic inference for dataclasses (astral-sh#18443)
  [ty] dataclasses: Allow using dataclasses.dataclass as a function. (astral-sh#18440)
  [ty] Create separate `FunctionLiteral` and `FunctionType` types (astral-sh#18360)
  [ty] Infer `list[T]` when unpacking non-tuple type (astral-sh#18438)
  [ty] Meta-type of type variables should be type[..] (astral-sh#18439)
  [`pyupgrade`] Make fix unsafe if it deletes comments (`UP050`) (astral-sh#18390)
  [`pyupgrade`] Make fix unsafe if it deletes comments (`UP004`) (astral-sh#18393)
  [ty] Support using legacy typing aliases for generic classes in type annotations (astral-sh#18404)
  Use ty's completions in playground (astral-sh#18425)
  Update editor setup docs about Neovim and Vim (astral-sh#18324)
  Update NPM Development dependencies (astral-sh#18423)
sharkdp added a commit that referenced this pull request Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants