-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Pyright doesn't respect the Python numbers hierarchy #1575
Comments
Pyright is doing the right thing here. The expression I confirmed that mypy reports the same errors. |
I was going to open an issue about how the following code seems to produce a false-positive from pyright from typing import Union
from numbers import Real
def f(x: Union[list, float]):
if not isinstance(x, Real):
y = len(x) # <- list | float cannot be assigned Sized But @erictraut you state that
However >>> from numbers import Real
>>> issubclass(float, Real)
True
>>> isinstance(float(2), Real)
True is there something that I am missing here? |
I found a relevant discussion on mypy, spurred by Guido. It looks like they created this open-since-2016 issue on python.typing. And it basically seems like "the
which makes me sad, because here I am needing to accommodate users who pass my function numpy-floats. Anyway, I figured I'd share some of my additional findings. Feel free to ignore all of this. |
Guido reversed his position on the numeric tower in 2020:
PEP 3141 is an accepted standard. NumPy, SymPy, and effectively all numeric frameworks comply with that standard. Ideally, It'd be kinda nice for the data science community if this could be reopened at some point. |
Pyright follows the type rules defined in PEP 484 (and follow-on typing PEPs) and the type information provided in typeshed stdlib stubs. If you think that the type information in those type stubs is incorrect, please file bugs or PRs in typeshed. If you think that changes are required to PEP 484 or other typing standards to accommodate your use case, then I recommend opening a discussion thread in the python/typing or posting to the typing-sig mailing list. |
Hmm. But PEP 3141 is an accepted standard. PEP 484 neither contravenes nor deprecates PEP 3141. Ideally, That said, I concede this might very well exceed As always, thanks for all static type-checking! |
PEP 3141 describes a number of abstract classes, and the type definitions of these classes are reflected in A type checker should not ignore type information in stdlib stubs and substitute its own understanding of how types work. There are a few exceptions where PEP 484 specifically calls out behaviors that cannot be specified in stubs and must be hard-coded in a type checker. The implicit relationship between If you think that the type information in If you think that the types described in PEP 3141 cannot be adequately described using the current typing standards, then we either need to augment the typing standards or carve out some special-case (hard-coded) behaviors that all standards-compliant type checkers must implement. Both of those would require discussions within the typing community. |
This functionality does not require hard-coding information, so it does not require a PEP. It requires pyright understand |
|
See python/mypy#2922 for discussion on this in mypy. I'm not sure there is a consensus that it shouldn't be supported, only that it would be difficult. |
Describe the bug
When using abstract types from
numbers
, Pyright doesn't recognize either literals (1.1
) or primitive types (float
) as compatible with the abstract bases likeNumber
,Integral
, etc.To Reproduce
Expected behavior
None of these statements would be type errors.
VS Code extension or command-line
VS Code extension, v1.1.117
Additional context
I'm not totally sure that this is pyright's fault—I don't really understand how
abc
andtyping
interact, and whether an abstract base class implicitly supports structural subtype-checking, or whether only explicit subclasses ofProtocol
support that. Maybe the solution here is actually some change tonumbers
in the standard library (or some additional stubs in typeshed).Though this might seem extremely pedantic, using types from
numbers
can be handy when you don't know (and don't care) if you'll be getting a Python type, or a scalar from NumPy.Union[int, float]
(correctly) rejects NumPy scalars. That said, of course the checker can't statically know if a NumPy scalar is Real vs Integral, so Number is probably the only practical type to use in that case.The text was updated successfully, but these errors were encountered: