-
Notifications
You must be signed in to change notification settings - Fork 5
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
Support for string type annotations in attrs #2
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
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,4 @@ | ||
[pytest] | ||
addopts = | ||
--ignore=tests/py36.py | ||
--ignore=tests/py37.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,26 @@ | ||
import attr | ||
|
||
import andi | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class A_36: | ||
b: 'B_36' | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class B_36: | ||
a: A_36 | ||
|
||
|
||
def cross_referenced_within_func(): | ||
|
||
@attr.s(auto_attribs=True) | ||
class A: | ||
b: 'B' | ||
|
||
@attr.s(auto_attribs=True) | ||
class B: | ||
a: A | ||
|
||
return andi.inspect(A.__init__), andi.inspect(B.__init__) |
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,13 @@ | ||
from __future__ import annotations # type: ignore | ||
|
||
import attr | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class A_37: | ||
b: B_37 | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class B_37: | ||
a: A_37 |
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
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,33 @@ | ||
import sys | ||
|
||
import pytest | ||
|
||
import andi | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 6), | ||
reason="Annotating the types of class variables require Python 3.6 or higher") | ||
def test_attrs_str_type_annotations_py36(): | ||
from py36 import A_36, B_36 | ||
assert andi.inspect(B_36.__init__) == {'a': [A_36]} | ||
assert andi.inspect(A_36.__init__) == {'b': [B_36]} | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 7), | ||
reason="'from __future__ import annotations' require Python 3.7 or higher") | ||
def test_attrs_str_type_annotations_py37(): | ||
from py37 import A_37, B_37 | ||
assert andi.inspect(B_37.__init__) == {'a': [A_37]} | ||
assert andi.inspect(A_37.__init__) == {'b': [B_37]} | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 6), | ||
reason="Annotating the types of class variables require Python 3.6 or higher") | ||
@pytest.mark.xfail | ||
def test_attrs_str_type_annotations_within_func_py36(): | ||
""" Andi don't work with attrs classes defined within a function. | ||
More info at: https://github.com/python-attrs/attrs/issues/593#issuecomment-584632175""" | ||
from py36 import cross_referenced_within_func | ||
a_inspect, b_inspect = cross_referenced_within_func() | ||
assert b_inspect['a'].__name__ == 'A' | ||
assert a_inspect['b'].__name__ == 'B' |
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.
@kmike an alternative to fix the attrs problem would be to provide a inspect for classes. Something like:
In this case I think we would have access to the global namespace easily. What do you think?