-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
support ForwardRef in Python 3.6 #706
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 ForwardRef in Python 3.6 #706
Conversation
Codecov Report
@@ Coverage Diff @@
## master #706 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 15 15
Lines 2721 2725 +4
Branches 539 539
=====================================
+ Hits 2721 2725 +4 |
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.
otherwise looks good to me.
tests/test_py36.py
Outdated
| @@ -0,0 +1,300 @@ | |||
| """ | |||
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.
looks like much of this might be duplicates of the python 3.7 tests? couldn't we just remove the "skip" and run the same test for both? (At least in some cases).
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.
Python 3.6 and pyhton3.7 are a little different (eg. _ForwardRef and ForwardRef).
I have to change exists test code.
I try to do it.
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.
great, I think better to have one set of tests than two, even if it means a bit of repeated try:...except: or if sys.version...else.
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 have changed test_py37.py to test_forward_ref.py and deleted test_py36.py
|
Does this mean, to support Python 3.6 in my code, I will need to add try:
from typing import ForwardRef
except ImportError:
from typing import _ForwardRef as ForwardRefanywhere I have need of a forward ref? Or will self-referencing strings still work fine? This makes me think it will, so I'm hoping so. e.g.this currently works for me in Python 3.7: from typing import List
from pydantic import BaseModel
class CalendarRule(BaseModel):
id: int
calendar: int
children: List["CalendarRule"]
ordering: int
rule_attribute: str
rule_value: str
CalendarRule.update_forward_refs()but I will not be able to reference it by string value, and instead will need to do: from typing import List
from pydantic import BaseModel
try:
from typing import ForwardRef
except ImportError:
from typing import _ForwardRef as ForwardRef
CalendarRule = ForwardRef("CalendarRule")
class CalendarRule(BaseModel):
id: int
calendar: int
children: List[CalendarRule]
ordering: int
rule_attribute: str
rule_value: str
CalendarRule.update_forward_refs() |
Yes, If you need
No, you don't need Also, you can install the PR version in your local machine to try it. |
| def test_forward_ref_dataclass(create_module): | ||
| module = create_module( | ||
| """ | ||
| from __future__ import annotations |
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 we still need a 3.7 only test with from __future__ import annotations to check that this works with future annotations.
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 have added unittest for python3.7
| from typing import List | ||
| from pydantic import BaseModel, Schema | ||
|
|
||
| class Account(BaseModel): | ||
| name: str | ||
| subaccounts: List[Account] = [] | ||
| subaccounts: List['Account'] = [] |
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.
again I'm afraid I think we need both this test and the 3.7 only test with from __future__ import annotations
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 add unittest for python3.7
tests/test_forward_ref.py
Outdated
| def test_circular_reference_json_schema(create_module): | ||
| module = create_module( | ||
| """ | ||
| from __future__ import annotations |
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.
and again.
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.
and I add unittest for python3.7.
tests/test_forward_ref.py
Outdated
| def test_self_forward_ref_collection(create_module): | ||
| module = create_module( | ||
| """ | ||
| from typing import ForwardRef, List, Dict | ||
| from typing import List, Dict | ||
| try: |
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.
might be good to add this to pydantic.utils.
Then you can do from pydantic.utils import ForwardRef in tests but also people can use that in their own code if they want to work with 3.6 and 3.7?
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, I add pydantic.utils.
They can use from pydantic.utils import ForwardRef with 3.6 and 3.7.
|
I have merged history :) |
|
great, thank you very much. |
|
Woohoo! Looking forward to v0.32 now! Thanks very much for this. |
Change Summary
Support ForwardRef in Python 3.6.
The Feature is talked in the issue. #463
The PR supports features on this list in Python 3.6
Related issue number
#463
Checklist
HISTORY.rsthas been updated#<number>@<whomever>