Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardizing Union of Literal and None #424

Closed
Avasam opened this issue Oct 16, 2023 · 7 comments · Fixed by #435
Closed

Standardizing Union of Literal and None #424

Avasam opened this issue Oct 16, 2023 · 7 comments · Fixed by #435

Comments

@Avasam
Copy link
Contributor

Avasam commented Oct 16, 2023

Literal["foo", None] is equivalent to Literal["foo"] | None and Optional[Literal["foo"]]. This came up a handful of times in my PR reviews on typeshed. I can still find usages of either form. Flake8-PYI should be able to detect simple Literal | None unions (ie: as long as it's not aliased) and could help standardize its usage, if this feature is wanted.

Ref. https://peps.python.org/pep-0586/#legal-parameters-for-literal-at-type-check-time

@srittau
Copy link
Collaborator

srittau commented Oct 16, 2023

If we do this, I'd prefer to standardize on Literal["foo"] | None. To me that's the most obvious form, especially since Literal[None] == None.

@AlexWaygood
Copy link
Collaborator

AlexWaygood commented Oct 16, 2023

I don't mind much either way, but I agree it would be nice for flake8-pyi to have an opinion on this, so we can standardise this across typeshed. Literal["foo"] | None is more verbose than Literal["foo", None], but it is true that None is very special, so I can definitely see the argument for the former version.

@Avasam
Copy link
Contributor Author

Avasam commented Oct 16, 2023

Some of my thoughts:

  • It's more verbose by only one character
  • The | union is more consistent with other Optional/None-able type unions. (Making it easier to search for and visually parse)
  • Whilst it's cool that the type system allows for Literal[None], it is surprising and I managed to forget about it more than once. (this point kinda becomes moot if a linter checks for it)

But I don't feel strongly either way.

@AlexWaygood
Copy link
Collaborator

Let's standardise on Literal["foo"] | None, then

@srittau
Copy link
Collaborator

srittau commented Oct 17, 2023

Also, it would be kind of strange (to me) to have an Union like Literal["", None] | int instead of Literal[""] | int | None.

@tomasr8
Copy link
Contributor

tomasr8 commented Oct 23, 2023

Hey again! I should have the time this week so I thought I'd take a stab at this (in case none of you is already working on it?)

@AlexWaygood
Copy link
Collaborator

Hey again! I should have the time this week so I thought I'd take a stab at this (in case none of you is already working on it?)

You're very welcome to it! :D

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

Successfully merging a pull request may close this issue.

4 participants