Skip to content

Fix Literal.__args__ attribute error - #22493

Open
zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/literal-args-attr-22491
Open

zhengdaodeguL wants to merge 1 commit into
python:masterfrom
zhengdaodeguL:fix/literal-args-attr-22491

Conversation

@zhengdaodeguL

Copy link
Copy Markdown

Fixes #22491

Accessing __args__ on a parameterized Literal[...] alias reported
"<typing special form>" has no attribute "__args__", even though the
attribute exists at runtime (and pyright/ty do not report an error).

alias_type_in_runtime_context returned a plain typing._SpecialForm
instance for parameterized special-form aliases, and the _SpecialForm
stub has no __args__ member. This change attaches a per-instance
__args__ attribute typed tuple[Any, ...] (via the existing
Instance.extra_attrs mechanism) for parameterized aliases only. Bare
special forms (Literal.__args__) and other missing attributes still
error as before.

Added testLiteralArgsAttribute to check-literal.test, covering the
issue repro (iterating Key.__args__), the revealed type, and the
negative cases.

Verified with pytest mypy/test/testcheck.py on the check-literal,
check-generics and check-type-aliases suites (405 passed, 3 skipped);
the new test fails on base code. Black and Ruff pass.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

spark (https://github.com/apache/spark)
+ python/pyspark/pandas/internal.py:768: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/pandas/internal.py:796: error: Unused "type: ignore" comment  [unused-ignore]

pydantic (https://github.com/pydantic/pydantic)
+ pydantic/plugin/_schema_validator.py:18: error: Unused "type: ignore" comment  [unused-ignore]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Literal[...].__args__ reports attribute error

1 participant