-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Type error in the section on type hints #120661
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
Comments
The simple fix, IMHO, is to replace kilograms with newtons. That's the correct SI base unit for force. You could use "pounds" or "lbs" but there are two problems with that. One, pounds is either a unit of force or unit of mass, depending on who you ask. Second, it's part of an obsolete unit system. ;-) |
The float typing has nothing to do with kilograms versus newtons. The sentence is an example of something many would actually write. This should be closed. |
Do we close this? |
I agree with changing this to avoid distracting readers who notice that the example is technically incorrect. The focus should be on the typing concepts, not the physics. I'd rather not use newtons as @nascheme suggests; far fewer readers will be familiar with the concept. Maybe instead use kilometer/miles conversion: def distance(kilometers: float) -> str:
return f"The distance is {kilometers} kilometers ({kilometers / 1.60934} miles)." |
(cherry picked from commit bb057ea) Co-authored-by: Bénédikt Tran <[email protected]>
(cherry picked from commit bb057ea) Co-authored-by: Bénédikt Tran <[email protected]>
Thanks for the fix @picnixz! Hopefully our code examples are now type-safe from wherever you are in the solar system 🤞 |
Uh oh!
There was an error while loading. Please reload this page.
Documentation
https://docs.python.org/3/library/typing.html
"This module provides runtime support for type hints.
Consider the function below:"
This is technically incorrect. Kilograms are not a measure of weight, they are a measure of mass. If you are 60 kilograms on earth, you are 60 kilograms on the moon, not 10 kilograms. You weigh six times less on the moon because the moon has six times less mass than the Earth, not because you have six times less mass on the moon.
On Earth, "kilogram" is a common shorthand for "the weight, on Earth, of a kilogram." On Earth, it is fine to drop the phrase "on Earth" because everybody understands we are talking about the weight of a kilogram on Earth. Once we are planet hopping, it no longer makes sense to use "kilogram" as shorthand for "weight of a kilogram" because the weight of a kilogram changes everywhere we go.
This is the kind of type error that some languages use type systems to prevent (https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/units-of-measure) so it makes sense to be careful about types here.
Linked PRs
The text was updated successfully, but these errors were encountered: