Skip to content

[ty] Use _cls as argument name for collections.namedtuple#24333

Merged
charliermarsh merged 1 commit intomainfrom
charlie/_cls
Mar 31, 2026
Merged

[ty] Use _cls as argument name for collections.namedtuple#24333
charliermarsh merged 1 commit intomainfrom
charlie/_cls

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

@charliermarsh charliermarsh commented Mar 31, 2026

@charliermarsh charliermarsh added the bug Something isn't working label Mar 31, 2026
@astral-sh-bot astral-sh-bot bot added the ty Multi-file analysis & type inference label Mar 31, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 31, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 86.61%. The percentage of expected errors that received a diagnostic held steady at 81.56%. The number of fully passing files held steady at 70/132.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 31, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 31, 2026

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-await 0 40 0
invalid-argument-type 0 4 0
missing-argument 0 3 0
parameter-already-assigned 0 3 0
invalid-return-type 0 1 0
Total 0 51 0

Changes in flaky projects detected. Raw diff output excludes flaky projects; see the HTML report for details.

Raw diff:

core (https://github.com/home-assistant/core)
- homeassistant/components/xbox/browse_media.py:33:12 error[missing-argument] No argument provided for required parameter `cls`
- homeassistant/components/xbox/browse_media.py:35:9 error[invalid-argument-type] Argument is incorrect: Expected `type[MediaTypeDetails]`, found `Literal[MediaClass.APP]`
- homeassistant/components/xbox/browse_media.py:35:9 error[parameter-already-assigned] Multiple values provided for parameter `cls`
- homeassistant/components/xbox/browse_media.py:37:13 error[missing-argument] No argument provided for required parameter `cls`
- homeassistant/components/xbox/browse_media.py:39:9 error[invalid-argument-type] Argument is incorrect: Expected `type[MediaTypeDetails]`, found `Literal[MediaClass.GAME]`
- homeassistant/components/xbox/browse_media.py:39:9 error[parameter-already-assigned] Multiple values provided for parameter `cls`

django-stubs (https://github.com/typeddjango/django-stubs)
- mypy_django_plugin/lib/helpers.py:226:16 error[missing-argument] No argument provided for required parameter `cls`
- mypy_django_plugin/lib/helpers.py:226:20 error[invalid-argument-type] Argument is incorrect: Argument type `Model` does not satisfy upper bound `DjangoModel` of type variable `Self`
- mypy_django_plugin/lib/helpers.py:226:20 error[invalid-argument-type] Argument is incorrect: Expected `type[DjangoModel]`, found `type[Model]`
- mypy_django_plugin/lib/helpers.py:226:20 error[parameter-already-assigned] Multiple values provided for parameter `cls`

Full report with detailed diff (timing results)

@charliermarsh charliermarsh marked this pull request as ready for review March 31, 2026 16:58
Comment on lines +1194 to +1195
Field names that would otherwise collide with the synthetic constructor receiver still work as
keyword arguments, matching CPython's generated `__new__(_cls, ...)` signature:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would just describe this with

Suggested change
Field names that would otherwise collide with the synthetic constructor receiver still work as
keyword arguments, matching CPython's generated `__new__(_cls, ...)` signature:
Regression test for https://github.com/astral-sh/ty/issues/3184: the first parameter of `__new__`
at runtime for a namedtuple class is `_cls`, meaning that `cls` can be used as a field name:

Comment on lines +1202 to +1208
PInfo(inst=None, cls=str)

class StaticInfo(NamedTuple):
inst: object | None
cls: type[str]

StaticInfo(inst=None, cls=str)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
PInfo(inst=None, cls=str)
class StaticInfo(NamedTuple):
inst: object | None
cls: type[str]
StaticInfo(inst=None, cls=str)
reveal_type(PInfo(inst=None, cls=str)) # revealed: PInfo
class StaticInfo(NamedTuple):
inst: object | None
cls: type[str]
reveal_type(StaticInfo(inst=None, cls=str)) # revealed: StaticInfo

@charliermarsh charliermarsh enabled auto-merge (squash) March 31, 2026 17:07
@charliermarsh charliermarsh merged commit a42d89b into main Mar 31, 2026
50 checks passed
@charliermarsh charliermarsh deleted the charlie/_cls branch March 31, 2026 17:12
carljm added a commit that referenced this pull request Apr 1, 2026
* main:
  [ty] Add missing test case for inline functional TypedDict with an invalid type passed to the `name` parameter (#24334)
  [ty] Use `_cls` as argument name for `collections.namedtuple` (#24333)
  [ty] Emit diagnostic for functional TypedDict with non-literal name (#24331)
  Add `nested-string-quote-style` formatting option (#24312)
  `RUF010`: Mark fix as unsafe when it deletes a comment
  [ty] Fix semantic token classification for properties accessed on instances (#24065)
  publish installers to `/installers/ruff/latest` on the mirror (#24247)
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.

namedtuple with cls in field names gives type warnings

3 participants