Skip to content

Do not allow type[] to contain Literal types#18276

Merged
sobolevn merged 5 commits into
masterfrom
issue-18196
Dec 11, 2024
Merged

Do not allow type[] to contain Literal types#18276
sobolevn merged 5 commits into
masterfrom
issue-18196

Conversation

@sobolevn

@sobolevn sobolevn commented Dec 10, 2024

Copy link
Copy Markdown
Member

Closes #18196

Comment thread mypy/types_utils.py
with recursive types like T = Type[T], because internal representation of
TypeType item is normalized (i.e. always a proper type).

Also forbids `Type[Literal[...]]`, because typing spec does not allow it.

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 think it might be better to frame this in terms of what is allowed. Here are some other things mypy allows that I don't think make sense: https://mypy-play.net/?mypy=latest&python=3.12&gist=aa900cb6c86fd55901b21ee182aa35ec

@sobolevn sobolevn Dec 10, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I suspected that this will be brought up :)
I would prefer to keep this PR focused on just one thing. Because, for example, I only agree with the first example: type[TypeGuard[int]], but it is a completely different thing to fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think that we can clarify this in https://github.com/python/typing/tree/main/docs/spec

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.

This is already specified in the grammar at https://typing.readthedocs.io/en/latest/spec/annotations.html#type-and-annotation-expressions . (Though I noticed we're missing unions there, which should be supported.)

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'm fine with deferring other similar changes to other PRs, though.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

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

bokeh (https://github.com/bokeh/bokeh)
+ src/bokeh/core/property/enum.py: note: In function "__init__":
+ src/bokeh/core/property/enum.py:64:30: error: type[...] can't contain "Literal[...]"  [valid-type]
+ src/bokeh/core/property/enum.py:70:36: error: type[...] can't contain "Literal[...]"  [valid-type]

@sobolevn
sobolevn merged commit 40730c9 into master Dec 11, 2024
@sobolevn
sobolevn deleted the issue-18196 branch December 11, 2024 08:10
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.

type[Literal['']] does not accept str as the type

2 participants