-
Notifications
You must be signed in to change notification settings - Fork 242
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
Clarify the float/int/complex special case #1746
Comments
We definitely need to change something here. The current wording implying that the special case only applies to parameter types does not describe the behavior of any current type checker, and I don't think it's a desirable behavior. We already have a test case for this section,
All type checkers tested pass this test, except pyre which fails to flag the Note the However, different type checkers actually interpret this case differently. Pyre and mypy treat the body of the I like the interpretation where the name |
I agree that this section should be clarified. These are sometimes referred to as "promotion types". Historically, About a year ago, I changed pyright's handling of promotion types. I now treat When compared to the more simplistic approach of always treating def func1(f: float):
if isinstance(f, float):
reveal_type(f) # pyright and mypy reveal "float"
else:
reveal_type(f) # pyright reveals "int", mypy considers this unreachable The downside to this approach is that pyright needs to internally distinguish between two variants of |
I agree with all the above. Assuming other typing council members feel similarly, I think that clarifies enough to allow a pull request to the spec. Side note: if we had intersection and negation type expressions, one could spell the "pure" float type as |
Yeah, I think KotlinIsland's union of ducklings approach better matches runtime behaviour than what mypy currently does |
Posted about this in https://discuss.python.org/t/clarifying-the-float-int-complex-special-case/54018. |
The typing spec currently says this:
This is helpful in that it clarifies that there is not a subtype relationship here. It remains unclear (to me, at least) in the following ways:
int
to afloat
typed argument, but then how do we type that name within the function body? If what we actually mean here is thatfloat
should be interpreted asfloat | int
, then we should say that clearly.There was some discussion of this on #1663, which is about the different but related question of whether to mention the numeric tower and PEP 3141.
The text was updated successfully, but these errors were encountered: