-
Notifications
You must be signed in to change notification settings - Fork 214
Provide type hints for type checkers and language servers #433
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
Hi, Sorry for not replying sooner. How would this look like? Currently python-stdnum still supports Python 2.7 (support will probably be dropped per Jan 2025). Also, can you describe what the benefit would be of type hints (for python-stdnum but also for users of the library)? Are there particular classes of bugs that are more easily found and fixed or is it mostly for API documentation purposes? Thanks, |
Hi, no worries. There's type comments that work with Python 2.7, that can easily be automatically converted to inline type annotations using something like pyupgrade later on. Here's an example from sentry's python client for how that would look like: But we could also just defer the inclusion of type hints until 2025 and maintain a separate stub package in typeshed until then. There's automated tooling to merge external stubs into inline annotations, so there would not be a lot of transition work required there either. In the case of this specific package the benefit in safety is in all likelihood very small, considering how simple the API is, but it might catch some problematic corner cases in the implementations here or there that would otherwise have only been caught by rigorous unit testing. The main benefit I see in language servers telling you what kind of objects the functions expect, but the API documentation would of course be improved as well by the inclusion of the type hint. |
@arthurdejong One other challenge with inline type hints will probably be that current type checkers and typeshed only support Python versions that aren't EOL, there's still some support for 3.8 in type checkers, but it will likely be gone by summer and there is no support for 3.6 and 3.7. So unless you're happy to drop support for Python 3.8 and below, we will need to weigh the costs of maintaining support for these versions. Maintaining compatibility with an old version of mypy that supports 3.6 at the same time as the latest version of mypy may prove challenging, although hopefully there are so few standard library dependencies, that we don't run into any compatibility issues. Thankfully mypy 1.5.1 isn't so old that I expect a ton of behavioral discrepancies, although there are some error codes that have been renamed/added/removed, so we wouldn't be able to use targeted Alternatively we could decide to no worry about supporting type hints in older Python versions. We would still need to make sure to document which things need to be imported from Also currently you're not putting a version gate into |
If you decide that hard-dropping support for anything below 3.6 or 3.9 is acceptable for the benefit of inline type hints, I'd be happy to work on a PR, so you can see how that would look. I don't expect it to be a lot of work, since a lot of the function definitions have the same signature, so the type hints can be generated automatically. |
While there are a lot of modules in this package, since the common interface is very simple from a type system perspective I think it would introduce little to no additional maintenance burden to include type hints and the majority of the initial hints could be added automatically for the common interface using something like
sed
orawk
.If there's any interest in maintaining inline type hints I'd be happy to create a pull request, if you'd rather not, I can go the typeshed route instead. I've already contributed type hints for various third party packages including Chameleon, WebOb, WTForms, uWSGI and gevent, just to name a few.
The text was updated successfully, but these errors were encountered: