8000 Add eval_type_backport to handle union operator and builtin generic subscripting in older Pythons by alexmojaki · Pull Request #8209 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
75986a9
Add eval_type_backport to handle union operator in older Pythons
alexmojaki Oct 29, 2023
ca60caa
Use modified get_type_hints in test_config
alexmojaki Oct 29, 2023
a163ea3
unskip a couple more tests in older pythons
alexmojaki Oct 29, 2023
8033556
Use pipe operator in a bunch of tests
alexmojaki Oct 29, 2023
0495f0f
various misc tidying up: use default localns=None, handle None values…
alexmojaki Nov 4, 2023
780b46a
explain asserts
alexmojaki Nov 4, 2023
4c536b6
type hints
alexmojaki Nov 4, 2023
d7f874b
inline node_to_ref
alexmojaki Nov 4, 2023
09d2e93
is_unsupported_types_for_union_error
alexmojaki Nov 4, 2023
d7b1462
tidying
alexmojaki Nov 4, 2023
c65ae3d
remove more type: ignore comments
alexmojaki Nov 4, 2023
3aa88da
docstrings and tidying
alexmojaki Nov 4, 2023
ca09a03
fix and tighten test_is_union
alexmojaki Nov 4, 2023
4890fc8
Merge branch 'main' of github.com:pydantic/pydantic into eval_type_ba…
alexmojaki Nov 22, 2023
c2d9d22
Use `eval_type_backport` package
alexmojaki Nov 22, 2023
40a41fb
Add test dependency
alexmojaki Nov 22, 2023
7da17e0
Merge branch 'main' of github.com:pydantic/pydantic into eval_type_ba…
alexmojaki Dec 16, 2023
a998d14
upgrade eval_type_backport
alexmojaki Dec 16, 2023
4f465a3
fix pdm.lock
alexmojaki Dec 16, 2023
71ca7cf
upgrade eval_type_backport to handle fussy typing._type_check
alexmojaki Dec 16, 2023
f060876
update is_backport_fixable_error and move down, update eval_type_back…
alexmojaki Dec 16, 2023
2b42d65
raise helpful error if eval_type_backport isn't installed. ensure tes…
alexmojaki Dec 17, 2023
dcbdd28
Restore skip, add another test for combination of backport and Pydant…
alexmojaki Dec 17, 2023
959c755
Test that eval_type_backport is being called in the right places
alexmojaki Dec 17, 2023
71c912e
test calling backport from get_type_hints
alexmojaki Dec 17, 2023
9847058
upgrade eval_type_backport to handle working union operator
alexmojaki Dec 17, 2023
6beab5b
unskip tests that can now pass in 3.8
alexmojaki Dec 17, 2023
b79692b
revert scattered test changes
alexmojaki Dec 17, 2023
6bc0ee4
unskip more tests
alexmojaki Dec 17, 2023
f423659
upgrade eval_type_backport to copy ForwardRef attributes, allowing un…
alexmojaki Dec 17, 2023
d3d5584
Merge branch 'main' of github.com:pydantic/pydantic into eval_type_ba…
alexmojaki Jan 15, 2024
aa21092
revert moving part of pyproject.toml
alexmojaki Jan 15, 2024
8da4294
Refine and test error raised when eval_type_backport isn't installed
alexmojaki Jan 16, 2024
60aa70f
use a type annotation that's unsupported in 3.9, not just 3.8
alexmojaki Jan 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docstrings and tidying
  • Loading branch information
alexmojaki committed Nov 10, 2023
commit 3aa88da7245f0b80b358ad1557ee69caceeb327a
22 changes: 14 additions & 8 deletions pydantic/_internal/_typing_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ def is_unsupported_types_for_union_error(e: TypeError) -> bool:
return str(e).startswith('unsupported operand type(s) for |: ')


class UnionTransformer(ast.NodeTransformer):
class _UnionTransformer(ast.NodeTransformer):
"""Transforms `X | Y` into `Union[X, Y]` if `X | Y` is not supported."""

def __init__(self, globalns: dict[str, Any] | None, localns: dict[str, Any] | None):
# This logic for handling Nones is copied from typing.ForwardRef._evaluate
if globalns is None and localns is None:
Expand All @@ -260,23 +262,23 @@ def eval_type(self, node: ast.AST) -> Any:

def visit_BinOp(self, node) -> ast.BinOp | ast.Subscript:
if isinstance(node.op, ast.BitOr):
left = self.visit(node.left)
right = self.visit(node.right)
left_val = self.eval_type(left)
right_val = self.eval_type(right)
left_node = self.visit(node.left)
right_node = self.visit(node.right)
left_val = self.eval_type(left_node)
right_val = self.eval_type(right_node)
try:
_ = left_val | right_val
except TypeError as e:
if not is_unsupported_types_for_union_error(e):
raise
# Replace `left | right` with `Union[left, right]`
# Replace `left | right` with `typing.Union[left, right]`
replacement = ast.Subscript(
value=ast.Attribute(
value=ast.Name(id=self.typing_name, ctx=ast.Load()),
attr='Union',
ctx=ast.Load(),
),
slice=ast.Index(value=ast.Tuple(elts=[left, right], ctx=ast.Load())),
slice=ast.Index(value=ast.Tuple(elts=[left_node, right_node], ctx=ast.Load())),
ctx=ast.Load(),
)
return ast.fix_missing_locations(replacement)
Expand All @@ -287,6 +289,10 @@ def visit_BinOp(self, node) -> ast.BinOp | ast.Subscript:
def eval_type_backport(
value: Any, globalns: dict[str, Any] | None = None, localns: dict[str, Any] | None = None
) -> Any:
"""Like `typing._eval_type`, but lets older Python versions use newer typing features.
Currently this just means that `X | Y` is converted to `Union[X, Y]` if `X | Y` is not supported.
This would also be the place to add support for `list[int]` instead of `List[int]` etc.
"""
try:
return typing._eval_type( # type: ignore
value, globalns, localns
Expand All @@ -295,7 +301,7 @@ def eval_type_backport(
if not (isinstance(value, typing.ForwardRef) and is_unsupported_types_for_union_error(e)):
raise
tree = ast.parse(value.__forward_arg__, mode='eval')
transformer = UnionTransformer(globalns, localns)
transformer = _UnionTransformer(globalns, localns)
tree = transformer.visit(tree)
return transformer.eval_type(tree)

Expand Down
0