Repository navigation
Conversation
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
| # typing errors should not be reported for specific modules. As we | ||
| # gradually make Astropy consistent with mypy's strict mode, we can | ||
| # remove the disabled error codes file-by-file. | ||
|
|
There was a problem hiding this comment.
Almost everything below here was created automatically.
There was a problem hiding this comment.
It would be great to add separation comments between the modules
e.g.
[mypy-astropy.conftest]
disable_error_code = arg-type,import-not-found
#####################################################################
[mypy-astropy.constants.codata2010]
disable_error_code = var-annotatedThere was a problem hiding this comment.
Should be pretty straightforward to do that. Glad we could hash this out! 🙃
|
👋 Thank you for your draft pull request! Do you know that you can use |
| enable_error_code = ignore-without-code | ||
|
|
||
| # We can get to passing mypy's strict mode by incrementally changing the | ||
| # following global settings to true. This comes from mypy's documentation. | ||
|
|
||
| warn_unused_configs = true | ||
| warn_redundant_casts = true | ||
| warn_unused_ignores = true | ||
| strict_equality = true | ||
| check_untyped_defs = false | ||
| disallow_subclassing_any = false | ||
| disallow_untyped_decorators = false | ||
| disallow_any_generics = false | ||
| disallow_untyped_calls = false | ||
| disallow_incomplete_defs = false | ||
| disallow_untyped_defs = false | ||
| no_implicit_reexport = false | ||
| warn_return_any = false | ||
| extra_checks = false |
There was a problem hiding this comment.
These settings come from [mypy's doc page on introducing enabling mypy on an existing code base]. The doc page on mypy configuration files describes the individual settings.
There was a problem hiding this comment.
My one larger suggestion is to have the configurations set on a per-subpackage basis rather than globally for all of Astropy.
@nstarman — just to make sure I understand, did you mean having a block of settings like this so that configuration variables like warn_redundant_casts, strict_equality, check_untyped_defs, etc. could be defined on a per-subpackage basis? I like that idea, and I think I'll probably apply that to PlasmaPy as well.
Alternatively, did you mean to have the per-file ignores become per-subpackage ignores like in #12971? That would also be a valid approach. The ignores would become less fine-grained, but mypy.ini would become shorter and probably more maintainable.
Thank you!
There was a problem hiding this comment.
The former 👍. I just imagine that some subpackages will achieve strict=True well before others.
nstarman
left a comment
There was a problem hiding this comment.
This is looking great. I'm happy with this type of incremental configuration.
My one larger suggestion is to have the configurations set on a per-subpackage basis rather than globally for all of Astropy. We will hopefully get to strict=True globally, but this is good for incremental typing.
| # typing errors should not be reported for specific modules. As we | ||
| # gradually make Astropy consistent with mypy's strict mode, we can | ||
| # remove the disabled error codes file-by-file. | ||
|
|
There was a problem hiding this comment.
It would be great to add separation comments between the modules
e.g.
[mypy-astropy.conftest]
disable_error_code = arg-type,import-not-found
#####################################################################
[mypy-astropy.constants.codata2010]
disable_error_code = var-annotated|
I think this approach makes a lot of sense. Looking forward, is there an option to detect automatically that a file actually does not have errors any more? A bit like an |
Thank you for the feedback! I don't know of a way to automatically check that a file actually does not have any errors anymore. In the short to intermediate term, it would probably the most sense to regenerate the file quasiperiodically. If we wanted to be fancy, we could create a GitHub Action as a cron job to create a PR that regenerates this file. I don't know if it would be worth the effort. The regeneration would probably be necessary because it seems like adding types to one fine can sometimes resolves errors in another file. The |
There was a problem hiding this comment.
I don't think this needs a change log. We don't do change log for black or ruff fixes.
| [mypy-astropy.coordinates.spectral_quantity] | ||
| disable_error_code = assignment,attr-defined | ||
|
|
||
| [mypy-astropy.coordinates.tests.accuracy.generate_ref_ast] |
There was a problem hiding this comment.
Do we want to type check the test files? I generally don't in my own code.
There was a problem hiding this comment.
me neither, but I think it's a good idea nevertheless: test code is a good source of user code, checking it might reveal that our type annotations are too strict compared to what we actually support and test
There was a problem hiding this comment.
Alternatively for the tests we could turn on beartype and do run-time checking of Astropy, checking all the type annotations without having to add type hints to the test suite.
from beartype.claw import beartype_this_package # <-- hype comes
beartype_this_package()
There was a problem hiding this comment.
I don't know that we need to add type annotations to tests but would definitely be good to have a way to have the tests actually test the annotations in the main code optionally.
There was a problem hiding this comment.
The bear-type route in the tests will accomplish this (testing the type hints without having to type the tests) and should only add a few lines to the test code + a test dependency. This also has the added benefit of testing runtime type checking! Which some users will definitely want.
There was a problem hiding this comment.
Interesting to hear about beartype!
For PlasmaPy, my current thinking is to include type hint annotations for tests since they can sometimes really help with test readability and maintainability...especially for when we're using @pytest.mark.parametrize or are somewhat complicated. I would be in favor of including type hint annotations for tests as well, but probably at a lower priority than for the application code.
There was a problem hiding this comment.
Adding type annotations to tests can be worthwhile if it helps IDEs offer better autocompletion.
|
A follow-up to this exploratory PR: I mentioned in this post that it's worth considering alternatives to mypy such as pyright. |
| examples | ||
| ) | ||
|
|
||
| enable_error_code = ignore-without-code |
There was a problem hiding this comment.
| enable_error_code = ignore-without-code | |
| enable_error_code = ignore-without-code | |
| plugins = numpy.typing.mypy_plugin |
There was a problem hiding this comment.
I think we will use this plugin frequently.
There was a problem hiding this comment.
There was a problem hiding this comment.
Making a note to add this to PlasmaPy!
|
Some follow-up thoughts about this PR after the discussion in #15170 and continuing to add type hints to PlasmaPy:
This would make the rollout of static type checking more gradual, which is key. |
|
Hi humans 👋 - this pull request hasn't had any new commits for approximately 4 months. I plan to close this in 30 days if the pull request doesn't have any new commits by then. In lieu of a stalled pull request, please consider closing this and open an issue instead if a reminder is needed to revisit in the future. Maintainers may also choose to add keep-open label to keep this PR open but it is discouraged unless absolutely necessary. If this PR still needs to be reviewed, as an author, you can rebase it to reset the clock. If you believe I commented on this pull request incorrectly, please report this here. |
|
This PR is superseded by #16312, which I think is a much better approach. |
Description
This prototype PR adds a mypy configuration file,
mypy.ini, that ignores existing errors on a per-file and per-error basis. When I runmypy .in the top-level directory of this repo with this file, it passes using mypy v1.8.0! 🎉 🎂 🥳 🎈 🎆 🥦The configuration file includes global settings near the top that can gradually be set to true in order to globally approach mypy's strict mode.
This PR does not add mypy to the suite of continuous integration tests, since doing so may require an Astropy Proposal for Enhancement (see astropy/astropy-APEs#92).
Motivation
Currently Astropy has no means to check that type hint annotations are correct, but there has been discussion over the last year or two about doing so, especially in #15170. If we want to methodically add accurate type hint annotations to Astropy, two steps that we'll need to take are:
This PR is a possible path forward for the first step, but there are other alternatives.
Backstory
This PR came about because I wrote a short script for PlasmaPy/PlasmaPy#2424 that parses the output of mypy and creates the sections in
mypy.inito ignore existing errors on a per-file and per-error basis. Since it was pretty straightforward to run for Astropy too, I decided to submit a demo/prototype PR.Consequences
Related issues
Closes #12970. This is similar to #12971, but this PR takes a much more fine-grained approach.