-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Recognize comparison with Ellipsis #2180
Comments
I believe we are already tracking this in #1803. |
#1803 only talks about Enums, not the Ellipsis constant. Is Ellipsis internally implemented as an Enum? |
The PEP just says "singleton types" and although it only gives None and enums as examples I think it applies here too. I'll add a note to #1803 just to be sure. Do note though, there is no actual type builtins.ellipsis at runtime (it's a figment of the stubs). Do you have to use Ellipsis for this? |
I tried to use
Since you're curious: The specific function that prompted this issue does an os.walk of a directory to locate all contained files. It returns a Mapping[FileName, Union[FilePath, builtins.ellipsis]] that allows O(1) mapping of a short filename to a filepath. If you do a get() on the dictionary there are 3 cases:
|
Hm. I recommend defining some dedicated constant for that. It doesn't look
like using Ellipsis here has advantages other than not needing to
define/import it.
Also note that the type of None is similarly hard to get at, but PEP 484
special-cases it. There are other similar singletons, e.g. NotImplemented.
All these probably deserve their types named and exported from either
builtins or types in Python itself (not just in the stubs).
|
I'd like to repoen this issue. Here is a legitimate example where a constant is not needed. def test() -> None:
arr = {} # type: Dict[str, Optional[List]]
val = arr.get('key', Ellipsis)
if val is Ellipsis:
return
if val is None:
val = [1, 2, 3]
arr['key'] = val
for i in val:
print(i)
Interestingly, using |
This is an incredibly specific and rare edge case. I wouldn't think that this merits custom type narrowing logic in a type checker — especially given that there is a simple workaround. if isinstance(val, ellipsis): |
The Ellipsis is just a placeholder, I do not specifically need the Ellipsis here. The only thing that bugs me is why does mypy not recognize if I specifically test for the same variable. And testing for |
Mypy evaluates types of expressions and symbols. It doesn't track specific values. Mypy knows that the type of If you want to avoid the additional import, you can do this: if isinstance(val, type(Ellipsis)): |
Consider the following program:
If I typecheck with:
I get:
But expect:
This suggests that mypy does not recognize the comparison
value is Ellipsis
in the same way that it recognizesvalue is None
.In my own codebase having explicit recognition for Ellipsis would be useful in some functions that use both None and Ellipsis as sentinel values.
The text was updated successfully, but these errors were encountered: