KEMBAR78
Do not allow `type[]` to contain `Literal` types by sobolevn · Pull Request #18276 · python/mypy · GitHub
Skip to content

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Dec 10, 2024

Closes #18196

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
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

Copy link
Member Author

@sobolevn sobolevn Dec 10, 2024

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
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
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
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
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
19 checks passed
@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