-
-
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 Console
and Unicode
unit formatters
#16465
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.
|
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.
All the annotations look good. A few suggestions for more.
astropy/units/format/console.py
Outdated
@@ -30,15 +40,15 @@ class Console(base.Base): | |||
_space = " " |
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 probably worth annotating these class variables as well.
_space = " " | |
_space: ClassVar[str] = " " |
@@ -43,7 +52,7 @@ def _format_unit_power(cls, unit, power=1): | |||
return name | |||
|
|||
@classmethod | |||
def _format_superscript(cls, number): | |||
def _format_superscript(cls, number: str) -> str: | |||
mapping = str.maketrans( |
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.
Unrelated (though perhaps this PR is as good a place as any), but this dict should be moved to outside this function.
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.
Pull requests that add type annotations should not include any code changes.
@@ -29,11 +38,11 @@ class Unicode(console.Console): | |||
_line = "─" |
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.
_line = "─" | |
_line: ClassVar[str] = "─" |
b81df21
to
8b13610
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.
One comment about class variables, but happy to just approve - this looks nice!
_times = "*" | ||
_line = "-" | ||
_space = " " | ||
_times: ClassVar[str] = "*" |
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 recall seeing some discussion by disgruntled typers about also typing variables, where what they should be is obvious and hence the typing only adds visual noise, with no benefit for humans. I'm happy to go with the flow here, but perhaps worth thinking about.
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 think these end up being quite useful because it's important to know when you're modifying a class-level variable. Lots of unexpected behavior can happen when changing a class variable and you think it's an instance variable.
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.
OK, that makes sense - and indeed these singleton classes are a bit odd.
scale: str, | ||
numerator: str, | ||
denominator: str, | ||
fraction: Literal[True, "inline", "multiline"] = "multiline", |
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.
Had to take a double-take, but, yes, the True
without a False
is indeed correct...
@pllim, can you merge or should I rebase to get the documentation build to succeed? |
@eerovaher , let's rebase to be sure. Thanks! |
8b13610
to
0044bbc
Compare
There shouldn't be anything holding this back anymore. |
Thanks for your patience! |
Description
This continues the work of adding type annotations to
units/format/
by annotating theConsole
formatter and theUnicode
formatter, which is a subclass ofConsole
.