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

Support Union narrowing with Ellipsis #4397

Closed
antdking opened this issue Jan 4, 2023 · 4 comments
Closed

Support Union narrowing with Ellipsis #4397

antdking opened this issue Jan 4, 2023 · 4 comments
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request

Comments

@antdking
Copy link

antdking commented Jan 4, 2023

Effectively the same as this issue from mypy

Ellipsis is a constant in Python, so we're able to do identity checks on it for narrowing.

This allows supporting using Ellipsis as a sentinel value where None is an accepted input.

basic example:

import types

# crude, but valid in more complex cases, such as a fallback when accessing a store
def get_val(default=...) -> int | types.EllipsisType:
    return default

x = get_val()

if x is not ...:
    reveal_type(x)

reveal_type output:

Pylance: Type of "x" is "int | ellipsis"
mypy(note): Revealed type is "builtins.int"
@erictraut
Copy link
Collaborator

Moving to pyright, since this is a core type checking issue.

@erictraut erictraut transferred this issue from microsoft/pylance-release Jan 4, 2023
erictraut pushed a commit that referenced this issue Jan 4, 2023
…...` and `x != ...`. Support for these were recently added to mypy. This addresses #4397.
@erictraut erictraut added enhancement request New feature or request addressed in next version Issue is fixed and will appear in next published version and removed triage-needed labels Jan 4, 2023
@erictraut
Copy link
Collaborator

This doesn't strike me as a common idiom in Python, but since support for this type guard was added to mypy recently, I think it's reasonable to add support in pyright as well.

This will be addressed in the next release.

@antdking
Copy link
Author

antdking commented Jan 4, 2023

Moving to pyright, since this is a core type checking issue.

Woops, sorry about that. Glad it's clean to move over.

This doesn't strike me as a common idiom in Python

So you have another usecase (outside of "mypy did it"), I did more digging.
Our wider usecase is related to Pydantic, and how it represents Required-Nullable fields. Though I think you'll only encounter it when building dynamic models.

Thanks for the quick fix on this!

@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.288, which I just published. It will also be included in a future release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants