-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add type annotations to introspection functions #16234
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.
|
|
||
from packaging.version import Version | ||
|
||
from .decorators import deprecated | ||
|
||
if TYPE_CHECKING: | ||
from types import FrameType, ModuleType |
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.
The documentation build failed because ModuleType
couldn't be found. Is there a way to set TYPE_CHECKING
to True
when building the documentation?
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.
Weird that it fails even with from __future__ import annotations
. Maybe quoting it would work ? (but you may also need noqa comments to prevent ruff from undoing it)
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.
RTD is complaining about ModuleType
, but not about FrameType
or Literal
. That's making me think that it's the fact I've used ModuleType
in a return annotation that's causing problems somehow.
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'm not comfortable enough around RTD to say anything with confidence, but this smells like a potential bug on their side ?
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.
It's part of a broader Sphinx issue sphinx-doc/sphinx#11991, a quick hacky way is to just add py:class ModuleType
to docs/nitpick-exceptions
.
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 tried a few different ways of importing ModuleType
, but I couldn't get the documentation build to work properly. It seems that adding a new nitpick exception is the optimal solution under the circumstances.
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.
Yeah, RTD support is suboptimal
@@ -86,7 +93,7 @@ def resolve_name(name, *additional_parts): | |||
return ret | |||
|
|||
|
|||
def minversion(module, version, inclusive=True): | |||
def minversion(module: ModuleType | str, version: str, inclusive: bool = True) -> bool: |
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.
thanks a lot for doing this, I can never remember the return type of this function, I'm thrilled that soon my text editor will tell me !
@@ -146,7 +153,9 @@ def minversion(module, version, inclusive=True): | |||
return Version(module_version) > Version(version) | |||
|
|||
|
|||
def find_current_module(depth=1, finddiff=False): | |||
def find_current_module( | |||
depth: int = 1, finddiff: bool | list[Literal[True] | str | ModuleType] = False |
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 would suggest splitting this line to make it clearer at a glance that there are just 2 arguments here
depth: int = 1, finddiff: bool | list[Literal[True] | str | ModuleType] = False | |
depth: int = 1, | |
finddiff: bool | list[Literal[True] | str | ModuleType] = False, |
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 have a weak preference for not splitting lines if we don't have to, but if someone else says they also prefer writing this out on two lines then I will make the change.
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.
IMO it's fine as is.
A Sphinx bug requires the documentation configuration to be updated too.
061da91
to
de682e6
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 haven't been much into typing so not sure if I am best person to review.
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.
Everything looks good. But it's strange that codecov is saying all the line changes aren't covered by CI!
Thanks, all! |
Description
All the functions defined in
astropy/utils/introspection.py
are now either annotated or deprecated.