-
Notifications
You must be signed in to change notification settings - Fork 306
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
Make @particle_input
compatible with from __future__ import annotations
#2479
Merged
namurphy
merged 11 commits into
PlasmaPy:main
from
namurphy:particle-input-from-future-annotations
Feb 1, 2024
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d7155ba
Add tests that should fail
namurphy fd89bda
Make failing tests fail the way they're supposed to
namurphy 0f3cf88
Use typing.get_type_hints
namurphy 383b7b3
Add changelog entry
namurphy 8de2df4
Add type hints & type ignore comments
namurphy 4f138fe
More type hint annotations
namurphy a50ee72
Merge branch 'main' of github.com:PlasmaPy/PlasmaPy into particle-inp…
namurphy 75268d1
Fix reStructuredText error
namurphy c41701d
Rename test file
namurphy 40a8ce1
Update changelog entry
namurphy 9c8f429
Update tests
namurphy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fixed a bug so that |particle_input| now works when a module begins | ||
with py:`from future import annotations`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
plasmapy/particles/tests/test_decorators_future_annotations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Tests for ``@particle_input`` with delayed evaluation of annotations.""" | ||
|
||
# These tests must be in their own file because the `from __future__` | ||
# import must be at the top. | ||
from __future__ import annotations | ||
|
||
from plasmapy.particles.decorators import particle_input | ||
from plasmapy.particles.particle_class import Particle, ParticleLike | ||
|
||
|
||
@particle_input | ||
def function_decorated_with_particle_input(particle: ParticleLike) -> Particle: | ||
return particle # type: ignore[return-value] | ||
|
||
|
||
class DecoratedClass: | ||
@particle_input | ||
def __init__(self, particle: ParticleLike) -> None: | ||
self.particle = particle | ||
|
||
|
||
class UndecoratedClass: | ||
@particle_input | ||
def decorated_method(self, particle: ParticleLike) -> Particle: | ||
return particle # type: ignore[return-value] | ||
|
||
|
||
def test_particle_input_from_future_import_annotations_function() -> None: | ||
particle = function_decorated_with_particle_input("p+") | ||
assert particle == Particle("p+") | ||
|
||
|
||
def test_particle_input_from_future_import_annotations_instantiation() -> None: | ||
instance = DecoratedClass("p+") | ||
|
||
assert isinstance(instance.particle, Particle) | ||
assert instance.particle == Particle("p+") | ||
|
||
|
||
def test_particle_input_from_future_import_annotations_method() -> None: | ||
instance = UndecoratedClass() | ||
result = instance.decorated_method(particle="p+") | ||
assert isinstance(result, Particle) | ||
assert result == Particle("p+") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ChatGPT suggested using
typing.get_type_hints
because other methods returned the string representations of the annotations rather than the actual objects. I don't know how long this would have taken me to figure out otherwise!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.
ChatGPT 😱