-
Notifications
You must be signed in to change notification settings - Fork 1
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
Type annotations throughout the engine for PyCharm #151
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Also fix a few warnings.
And fix a few warnings.
pmer
force-pushed
the
pmer/type-annotations
branch
from
May 8, 2017 03:51
8fd5b1a
to
7e24772
Compare
Fixed two bugs found by the typechecker.
The layer() method's documentation is incorrect. Either its documentation or its implementation need to change.
Type checking revealed that __check_suppressed could not supress non-string objects passed to msg and info. Fixed that.
Plus some minor cleanup.
pmer
force-pushed
the
pmer/type-annotations
branch
from
May 8, 2017 03:56
7e24772
to
98bb029
Compare
pmer
changed the title
Add type annotations to engine (but not world... yet)
Type annotations throughout the engine for PyCharm
May 8, 2017
I like the way this looks! |
Oh yeah if a parameter is annotated by the programmer with some type T but it has a default value of None then its type is automatically upgraded to Optional[T] during type checking. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related to #142 and #148.
While adding type annotations, PyCharm showed some new highlights in the code that contained a few bugs so I fixed them. \o/
Some modifications were made to allow the type checker to work:
CHECK
orCheckFailure
nowimport
it from __main__.All the errors that PyCharm was throwing around from the
CHECK()
calls being undefined disappeared onceCHECK
was imported rather than being passed through the builtins.Why is this useful by itself?
It causes PyCharm to highlight more types of logic errors while working on the engine. It is useful to engine developers.
What do static type check errors look like?
But it's not perfect:
Ideally that should have shown an error. PEP 526 solves this but it requires Python 3.6. We can poke around with that if you want whenever we next upgrade the minimum language version.
How do I write my own type annotations?
This is explained in detail by Guido in PEP 484 but I'll try to condense the high level ideas below.
self
parameter on methods, though. Annotations are completely optional (like documentation!), so if you leave them out nothing bad will happen. But if you fill them in we'll get static type checks within that function and on all calls to that function.str
,float
,int
,list
(a list with no further restrictions),dict
, etc. Or it can be the valueNone
which stands for the type of the None value. Or it can be a type from the newtyping
module introduced with Python 3.5. The types from this module allow more expression so you can writeList[int]
(a list that is restricted to holding ints),Union[str, int]
(something that can either be a string or an int) and so on. Thetyping
module providesOptional[T]
as shorthand forUnion[T, None]
since that's so common. This means something with theOptional[str]
type can either beNone
or astr
. Also, you can just writefloat
in places where you'd ordinarily want to writeUnion[int, float]
since PEP 484 says otherwise there'd be pretty much no reason to just usefloat
by itself! Finally, an annotation can be a class that is in the current scope, either because it was declared earlier in the file above or a Python class that was imported from another file.Can I have a tool write the type annotations for me?
Yes, PyCharm and Pytype do this. The former is the easier of the two to use.
Next steps
My plans for after this PR.