You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
test_recognize_ellipsis_comparison.py:8: error: Revealed type is 'builtins.str'
test_recognize_ellipsis_comparison.py:15: error: Revealed type is 'Union[builtins.str, builtins.ellipsis]'
But expect:
test_recognize_ellipsis_comparison.py:8: error: Revealed type is 'builtins.str'
test_recognize_ellipsis_comparison.py:15: error: Revealed type is 'builtins.str'
This suggests that mypy does not recognize the comparison value is Ellipsis in the same way that it recognizes value 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 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?
Do note though, there is no actual type builtins.ellipsis at runtime (it's a figment of the stubs).
I tried to use type(Ellipsis) but mypy's parser choked on it. Is there a more reliable symbol than builtins.ellipsis, which apparently is an implementation detail?
Do you have to use Ellipsis for this?
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:
None: No file with the specified name exists.
Ellipsis: Multiple files with the specified name exist. An ambiguous reference.
FilePath: There is a unique file with the specified name and this is its path.
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).
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.
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 isinstance(ellipsis) requires an additional import whereas Ellipsis is built-in. But if you say it is not going to change, then fine.
Mypy evaluates types of expressions and symbols. It doesn't track specific values. Mypy knows that the type of val is builtin.ellipsis, but it doesn't know that it is the same value as Ellipsis. We happen to know that it's the same value (the same instance), but a static type checker cannot make that assumption.
If you want to avoid the additional import, you can do this:
Activity
gvanrossum commentedon Sep 24, 2016
I believe we are already tracking this in #1803.
davidfstr commentedon Sep 25, 2016
#1803 only talks about Enums, not the Ellipsis constant. Is Ellipsis internally implemented as an Enum?
gvanrossum commentedon Sep 25, 2016
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?
davidfstr commentedon Sep 25, 2016
I tried to use
type(Ellipsis)but mypy's parser choked on it. Is there a more reliable symbol thanbuiltins.ellipsis, which apparently is an implementation detail?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:
gvanrossum commentedon Sep 25, 2016
relikd commentedon Apr 12, 2022
I'd like to repoen this issue. Here is a legitimate example where a constant is not needed.
Interestingly, using
NotImplementedis fine but I don't want to use non-descriptive and potentially misleading flags solely to make mypy happy.erictraut commentedon Apr 12, 2022
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.
relikd commentedon Apr 12, 2022
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
isinstance(ellipsis)requires an additional import whereasEllipsisis built-in. But if you say it is not going to change, then fine.erictraut commentedon Apr 12, 2022
Mypy evaluates types of expressions and symbols. It doesn't track specific values. Mypy knows that the type of
valisbuiltin.ellipsis, but it doesn't know that it is the same value asEllipsis. We happen to know that it's the same value (the same instance), but a static type checker cannot make that assumption.If you want to avoid the additional import, you can do this: