-
Notifications
You must be signed in to change notification settings - Fork 15
Complete typing with strict type-checking #43
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
Complete typing with strict type-checking #43
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running on tests is important too. This ensures expected usage is correctly typed. In fact, it found that parse was missing the int annotation on its first parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows the use of | in Pyton < 3.10, and use symbols that won't be present at runtime, in annotations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as imports without renaming is a typing convention understood by type-checkers to mean that the symbol is purposefully / explicitly re-exported (the other option is to use __all__)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By using an unpacked TypedDict to annotate kwargs, you don't have to duplicate all your default keyword argument parameter's default values to have these functions fully typed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment in datemath/__init__.py
total=False means all members are optional (since we use this to annotate optional kwargs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In strict mode, mypy will whine about returning Any from a typed function.
This can either be cast or type-ignored to accept the "type unsafety".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Less runtime casting in some cases. But more importantly, this is now type-safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arrow types these only with the literal strings they accept. I figured you'd probably prefer keeping the types and checks simpler with a more general str, as any usage of roundDate would also have to comply to only using statically known literals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unittest2 is untyped, but also no longer needed without Python 2 support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is going to get removed in the next release anyways since this package is only going to be python3 going forward
2fa344b to
c90f0bc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In strict mode, mypy will whine about returning Any from a typed function.
This can either be cast or type-ignored to accept the "type unsafety".
c90f0bc to
5d1bb8f
Compare
Thank you so much for your contribution @Avasam ! This is quite awesome!! I've been writing so much golang lately that I have taking the typing for granted and it seems folks really needed it for this module. I'll approve it and merge it. I'll get a release out soon. There are few other things I'd like to tidy up before the next one first related to the legacy python 2.7 support we had.
5d1bb8f to
360cc11
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd personally bump mypy all the way to latest. 1.7 is the minimum where Unpack is no longer an incomplete feature https://mypy-lang.blogspot.com/2023/11/mypy-17-released.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this. mypy is only used in this package to do the type checking so its probably good that its at the latest anyways. That got the tests to pass now
Uh oh!
There was an error while loading. Please reload this page.
Closes #42
I figured this library is small enough I could lend a hand.
This is everything to have complete type annotations, be on par with typeshed's stubs, and use strict type-checking. I can split into smaller PRs if needed.
More details in GitHub file comments.