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
Incrementally expanding the use of mypy #28833
Comments
Is it possible to lint for type annotations so that they're only used where we actually run mypy? I noticed there's at least |
Note that a lot of WebDriver tests originate in vendor patches. So it's exactly the kind of code where being able to run the typecheck as part of vendor CI using the exact same version of mypy that we use elsewhere would be most worthwhile. That seems like a reasonably complex problem and so isn't the first thing I'd tackle. Also (hopefully) tests are faily small and self contained, so figuring out the code directly isn't too bad (I don't think this is always true but it's a nice idea :) ) Practically I think the largest benefit in the short term is going to come from extending the use of mypy to cover more of the tools/ directory. I'm not so worried about the vendor CI problem there since it's a much smaller group of contributors and they mostly know how to interact with GitHub. Making wptserve and wptrunner a) pass typechecks and b) actually have type annotations seems like the right next step. I'd guess it's going to be a few weeks of work at least; some Python patterns are easy to express in code and hard to express in types, and I've previously found during this kind of conversion that I just rewrote code to be less dynacially typed rather than trying to figure out how to make mypy happy. |
Oh and for linting, I'm pretty sure we could write a lint using the python tokenize module (or build a full ast if needed) to find things that look like type annotations outside a specific list of directories. It's not going to be trivial, but it feels possible at least. |
How big of a step would it be to run mypy on all of tools/ but only have the strict options apply to the parts of the code that it currently runs on (manifest+lint+gitignore)? |
You could rename |
I gave this a shot today, and with not too much fixing of fatal errors it's possible to run mypy for all of tools/ with a bunch of packages explicitly ignored, and some paths not checked at all. I'll send PRs of minor things that are blocking with reference to this issue. |
The type annotations here aren't very useful, but simple to do and avoids the need to exempt this script from mypy later. It's been tested with mypy locally. Part of #28833.
I'm not convinced it's overly worthwhile to actually forbid them elsewhere, tbh? But I do think getting all of tools/* covered would be good. (And we can use the Py3 syntax now! 💜) |
OK, it was hairy to get done but the result looks clean enough: #28900 It depends on some other changes, so I'm leaving it as a draft. Enough mypy for today I think :) |
The type annotations here aren't very useful, but simple to do and avoids the need to exempt this script from mypy later. It's been tested with mypy locally. Part of #28833.
…ved between Python 2 and 3, a=testonly Automatic update from web-platform-tests Simplify try-except imports of things moved between Python 2 and 3 (#28894) It's not clear from the code alone which is which, but these import statements work on Python 3.8.2: ```py from html import escape import queue from urllib.request import urlopen ``` Part of web-platform-tests/wpt#28776. Part of web-platform-tests/wpt#28833. -- wpt-commits: 20e07ca88113ef6132c4756019e4dd4ab6c46f0a wpt-pr: 28894
…notations, a=testonly Automatic update from web-platform-tests Update retry.py to Python 3 with type annotations (#28895) The type annotations here aren't very useful, but simple to do and avoids the need to exempt this script from mypy later. It's been tested with mypy locally. Part of web-platform-tests/wpt#28833. -- wpt-commits: 4bc9779398edf5c66d7630830e384ba774c5969e wpt-pr: 28895
This code was already mostly annotated, but needs updating to pass our mypy.ini rules. A few changes are noteworthy: - QuicTransportProtocol.streams was removed rather than annotated, because it's actually unused. - An assert for self.handler is added after process_client_indication() to it clear that that it can't be None in the following loop. Part of #28833. Depends on #28900.
Changes are as minimal as possible. The annotation for Kwargs in tools/wpt/utils.py was invalid syntax. `class Kwargs(Dict[Any, Any])` would work but doesn't add any type information, so not done. __init__.py files were added to various test directories so that they're treated as packages, similar to tools/lint/tests/__init__.py and other existing test directories in mypy-checked code. Part of #28833.
This code was already mostly annotated, but needs updating to pass our mypy.ini rules. A few changes are noteworthy: - QuicTransportProtocol.streams was removed rather than annotated, because it's actually unused. - An assert for self.handler is added after process_client_indication() to it clear that that it can't be None in the following loop. Part of #28833. Depends on #28900.
Make ConfigDict a Dict[str, Any] since the keys come from parser.options() which are strings in typeshed: https://github.com/python/typeshed/blob/7a9b4f93baa03fff3ef7f319988cea2cbb6e2135/stdlib/configparser.pyi#L105 Also s/SafeConfigParser/ConfigParser/ since SafeConfigParser is now a legacy alias not mentioned in Python 3 documentation. Part of #28833.
Make ConfigDict a Dict[str, Any] since the keys come from parser.options() which are strings in typeshed: https://github.com/python/typeshed/blob/7a9b4f93baa03fff3ef7f319988cea2cbb6e2135/stdlib/configparser.pyi#L105 Also s/SafeConfigParser/ConfigParser/ since SafeConfigParser is now a legacy alias not mentioned in Python 3 documentation. Part of #28833.
Automatic update from web-platform-tests Enable mypy for tools/quic/ (#28916) This code was already mostly annotated, but needs updating to pass our mypy.ini rules. A few changes are noteworthy: - QuicTransportProtocol.streams was removed rather than annotated, because it's actually unused. - An assert for self.handler is added after process_client_indication() to it clear that that it can't be None in the following loop. - Similar asserts are added for origin_string and path_string Part of web-platform-tests/wpt#28833. -- wpt-commits: f137686dc4fdc5cab4018c0c450c655d61cf47ef wpt-pr: 28916
…untyped defs), a=testonly Automatic update from web-platform-tests Run mypy for tools/ci/ (but still allow untyped defs) (#28952) Part of web-platform-tests/wpt#28833. -- wpt-commits: 96d6ddeceb9e8a755e9d2fe95197fc45a3486118 wpt-pr: 28952
…w untyped defs), a=testonly Automatic update from web-platform-tests Run mypy for tools/wave/ (but still allow untyped defs) (#28987) Part of web-platform-tests/wpt#28833. -- wpt-commits: 0b4f07772e96d0fc78558fe414571d7243aeaa9a wpt-pr: 28987
Make ConfigDict a Dict[str, Any] since the keys come from parser.options() which are strings in typeshed: https://github.com/python/typeshed/blob/7a9b4f93baa03fff3ef7f319988cea2cbb6e2135/stdlib/configparser.pyi#L105 Also s/SafeConfigParser/ConfigParser/ since SafeConfigParser is now a legacy alias not mentioned in Python 3 documentation. Part of #28833.
…ow untyped defs), a=testonly Automatic update from web-platform-tests Run mypy for tools/serve/ (but still allow untyped defs) Part of web-platform-tests/wpt#28833. -- wpt-commits: 9cbcc977a22a6fe04af23be0876823fb5d5c3d8f wpt-pr: 28988
…ow untyped defs), a=testonly Automatic update from web-platform-tests Run mypy for tools/serve/ (but still allow untyped defs) Part of web-platform-tests/wpt#28833. -- wpt-commits: 9cbcc977a22a6fe04af23be0876823fb5d5c3d8f wpt-pr: 28988
Closing this, mypy is now running with optional typing in all of tools/. Getting 100% type coverage is a whole other story, and probably not worth pushing for. |
…allow untyped defs), a=testonly Automatic update from web-platform-tests Run mypy for tools/wptserve/ (but still allow untyped defs) (#28954) Part of web-platform-tests/wpt#28833. -- wpt-commits: 2b537ce2f51488259779a2f78c702e368030a6dc wpt-pr: 28954
…allow untyped defs), a=testonly Automatic update from web-platform-tests Run mypy for tools/wptserve/ (but still allow untyped defs) (#28954) Part of web-platform-tests/wpt#28833. -- wpt-commits: 2b537ce2f51488259779a2f78c702e368030a6dc wpt-pr: 28954
Creating a new issue to consolidate discussion.
I asked in #28815:
@jgraham wrote in #28757 (comment):
https://mypy.readthedocs.io/en/stable/existing_code.html has some useful guidance, but we'd still need to figure out most of the hard problems stemming from downstream changes ourselves.
Let's treat the problem of tools/ and everything else separately. Outside of tools/, my thoughts are:
The text was updated successfully, but these errors were encountered: