Skip to content

units: allow return type annotations to contain non-unit types#11506

Merged
mhvk merged 2 commits into
astropy:mainfrom
noc0lour:return_type_annotations_nonunit
Apr 10, 2021
Merged

units: allow return type annotations to contain non-unit types#11506
mhvk merged 2 commits into
astropy:mainfrom
noc0lour:return_type_annotations_nonunit

Conversation

@noc0lour

@noc0lour noc0lour commented Apr 9, 2021

Copy link
Copy Markdown
Contributor

Description

This pull request is provides a small, backportable bugfix to allow non-unit types to be used as return type annotations.
These non-unit types are then ignored by @quantity_input decorators, such that no conversion is attempted.

Fixes #10156

@github-actions github-actions Bot added the units label Apr 9, 2021
@pllim pllim added the Bug label Apr 9, 2021
@pllim

pllim commented Apr 9, 2021

Copy link
Copy Markdown
Member

Thanks! Given this is a bug, will need a change log too. We have changed the change log format, see https://github.com/astropy/astropy/blob/main/docs/changes/README.rst

@pllim pllim added this to the v4.3 milestone Apr 9, 2021
@pllim pllim requested review from adrn and mhvk April 9, 2021 13:35
@pllim

pllim commented Apr 9, 2021

Copy link
Copy Markdown
Member

I am not convinced this needs a backport but reviewers will change the milestone if they disagree.

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is easy to review and I now definitely agree we may as well fix this, even though the larger reorganization by @nstarman may change it slightly. (@nstarman - your work would not change the outcome of the test, correct?)

Since all I have is a nitpick, I'll approve (but leaving it open for a little while for possible other comments)

Comment thread astropy/units/decorators.py Outdated
if wrapped_signature.return_annotation not in valid_empty:
if (wrapped_signature.return_annotation not in valid_empty) and isinstance(
wrapped_signature.return_annotation, (str, UnitBase, FunctionUnitBase)
):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Total nitpick, but could the closing parenthesis be on the previous line? I still find this aspect of blacken really unpythonic and cluttery... (Also, beyond PEP8 our general request is to follow the style of existing code in a file)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of any string, the code should check for a valid physical type. mypy and similar allow for a string annotation to be used instead of a class, where the class has not yet been defined.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, yes, but I think here I think it is fine to be simple.

But, now thinking about it: since the return annotation should normally be a class if it is not a unit, could one just do not isinstance(..., type)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be a problem starting in python 3.10, but it can be problematic here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be good enough 👍

def func(x) -> int:
    pass

isinstance(func.__annotations__["return"], type)
>>> True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait! further tests are problematic

@nstarman nstarman Apr 9, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import typing

class A:
    pass

def func(x) -> typing.Type[A]:
    return A

isinstance(func.__annotations__["return"], type)
>>> False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation appears the safest.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, @noc0lour, let's just stick with what you have. Could you still change the parenthesis back to the previous line? You can add [skip ci] to the commit to avoid rerunning all the tests. Happy to merge with that!

@nstarman

nstarman commented Apr 9, 2021

Copy link
Copy Markdown
Member

(@nstarman - your work would not change the outcome of the test, correct?)

@mhvk, nope! no change.

@mhvk

mhvk commented Apr 10, 2021

Copy link
Copy Markdown
Contributor

Oops, just noticing @pllim's comment - yes, please do add a changelog fragment as well! This is a nicely solves something that has bugged people before, after all!

@noc0lour

Copy link
Copy Markdown
Contributor Author

Alright, I'll change the parenthesis & add the changelog in a bit. Thanks for the quick review :)

This commit tests the type of the return type annotation
and only performs conversion if the type is either `str`
or a Unit type. This puts the return type annotations in
line with other annotations.

[skip ci]
@noc0lour noc0lour force-pushed the return_type_annotations_nonunit branch from 680188a to fe6a47c Compare April 10, 2021 13:14

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks all OK now, so will approve and merge...

@mhvk mhvk merged commit 99b1f53 into astropy:main Apr 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow return annotations other than Quantity

4 participants