Skip to content

Conversation

kianelbo
Copy link
Contributor

closes #13676

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Aug 27, 2025
Comment on lines 2026 to 2027
if annotation is not sig.empty and annotation != inspect._empty:
return inspect.formatannotation(annotation).replace("'", "")
Copy link
Member

Choose a reason for hiding this comment

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

This seems odd to me, for various reasons:

  • inspect._empty is private so it shouldn't be used
  • inspect.formatannotation, while public, is undocumented so probably shouldn't be used either
  • Why remove ' from it, which is something inspect itself doesn't seem to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that inspect._empty condition is actually redundant. I investigated the logic further and there were lots of inconsistent behaviors in the test (that's why I previously did replace("'", "")) and corner cases. However, the new solution (though a bit hacky) is the most general one and hopefully covering everything.

@@ -0,0 +1 @@
Added return type annotations in ``fixtures`` and ``fixtures-per-test``.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Added return type annotations in ``fixtures`` and ``fixtures-per-test``.
Added return type annotations in ``--fixtures`` and ``--fixtures-per-test``.

if annotation.__module__ == "typing":
return str(annotation).replace("typing.", "")
return annotation.__name__
except (ValueError, TypeError):
Copy link
Member

Choose a reason for hiding this comment

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

Why would ValueError and TypeError happen here? The only thing that could happen I think is AttributeError, which already happens with e.g. -> None: as annotation.

That being said, None should definitively handled correctly by the code and tested properly as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TypeError happens when the argument is not a callable (e.g. a number). I doubt this case happens. ValueError happens when the passed argument is a callable but a signature can't be obtained (e.g. range). Also unlikely imo.
For -> None: on my local test didn't raise any error.

@@ -5068,3 +5088,45 @@ def test_method(self, /, fix):
)
result = pytester.runpytest()
result.assert_outcomes(passed=1)


def test_get_return_annotation() -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe turn this into a test class and split the individual asserts into separate test functions? That way, if one of them fails, the rest of the tests will still run.


assert get_return_annotation(no_annotation) == ""

def none_return() -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Ah, this test happens to work (despite the bug above) because this module uses from __future__ import annotations, so the annotation already is 'None' rather than None.

Not sure how to best get around this. Maybe those tests should be in a separate module which deliberately doesn't use that?

Copy link
Contributor Author

@kianelbo kianelbo Aug 28, 2025

Choose a reason for hiding this comment

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

On my local test (3.13) with or without from __future__ import annotations it worked the same. I might be very wrong but I think it behaves differently in different Python versions. Still, I added a exclusive check for none. Do you think moving it to another module is worth it? Seems too much for a humble helper function!

PS: The reason I didn't use `isinstance(annotation, types.NoneType) was that pylance was complaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided (automation) changelog entry is part of PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show return type annotations in --fixtures and --fixtures-per-test
2 participants