Skip to content

Commit

Permalink
Fix imports indirectly from pydantic typing (#4358)
Browse files Browse the repository at this point in the history
* Fix imports indirectly from pydantic typing

* fix pipeline

* Add MappingIntStrAny to __all__

* fix ForwardRef

* Fix Callable

* remove from ForwardRef imports

* Add changes
  • Loading branch information
aminalaee committed Aug 11, 2022
1 parent 1e821b6 commit 1f83221
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 27 deletions.
1 change: 1 addition & 0 deletions changes/4358-aminalaee.md
@@ -0,0 +1 @@
Fix implpicitly importing `ForwardRef` and `Callable` from `pydantic.typing` instead of `typing` and also expose `MappingIntStrAny`.
5 changes: 2 additions & 3 deletions pydantic/fields.py
@@ -1,13 +1,14 @@
import copy
from collections import Counter as CollectionCounter, defaultdict, deque
from collections.abc import Hashable as CollectionsHashable, Iterable as CollectionsIterable
from collections.abc import Callable, Hashable as CollectionsHashable, Iterable as CollectionsIterable
from typing import (
TYPE_CHECKING,
Any,
Counter,
DefaultDict,
Deque,
Dict,
ForwardRef,
FrozenSet,
Generator,
Iterable,
Expand All @@ -32,8 +33,6 @@
from .errors import ConfigError, InvalidDiscriminator, MissingDiscriminator, NoneIsNotAllowedError
from .types import Json, JsonWrapper
from .typing import (
Callable,
ForwardRef,
NoArgAnyCallable,
convert_generics,
display_as_type,
Expand Down
2 changes: 1 addition & 1 deletion pydantic/schema.py
Expand Up @@ -11,6 +11,7 @@
Any,
Callable,
Dict,
ForwardRef,
FrozenSet,
Generic,
Iterable,
Expand Down Expand Up @@ -66,7 +67,6 @@
constr,
)
from .typing import (
ForwardRef,
all_literal_values,
get_args,
get_origin,
Expand Down
3 changes: 1 addition & 2 deletions pydantic/typing.py
Expand Up @@ -268,8 +268,6 @@ def is_union(tp: Optional[Type[Any]]) -> bool:
AnyClassMethod = classmethod[Any]

__all__ = (
'ForwardRef',
'Callable',
'AnyCallable',
'NoArgAnyCallable',
'NoneType',
Expand Down Expand Up @@ -308,6 +306,7 @@ def is_union(tp: Optional[Type[Any]]) -> bool:
'get_all_type_hints',
'is_union',
'StrPath',
'MappingIntStrAny',
)


Expand Down
2 changes: 1 addition & 1 deletion pydantic/validators.py
Expand Up @@ -12,6 +12,7 @@
Callable,
Deque,
Dict,
ForwardRef,
FrozenSet,
Generator,
Hashable,
Expand All @@ -30,7 +31,6 @@
from .datetime_parse import parse_date, parse_datetime, parse_duration, parse_time
from .typing import (
AnyCallable,
ForwardRef,
all_literal_values,
display_as_type,
get_class,
Expand Down
3 changes: 1 addition & 2 deletions tests/mypy/modules/success.py
Expand Up @@ -7,7 +7,7 @@
import os
from datetime import date, datetime, timedelta
from pathlib import Path, PurePath
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, ForwardRef, Generic, List, Optional, TypeVar
from uuid import UUID

from typing_extensions import TypedDict
Expand Down Expand Up @@ -47,7 +47,6 @@
)
from pydantic.fields import Field, PrivateAttr
from pydantic.generics import GenericModel
from pydantic.typing import ForwardRef


class Flags(BaseModel):
Expand Down
27 changes: 11 additions & 16 deletions tests/test_forward_ref.py
@@ -1,5 +1,5 @@
import sys
from typing import Dict, List, Optional, Tuple
from typing import Dict, ForwardRef, List, Optional, Tuple

import pytest

Expand Down Expand Up @@ -66,8 +66,6 @@ class Bar(BaseModel):
"""
)

from pydantic.typing import ForwardRef

assert module.Foo.__fields__['a'].type_ == ForwardRef('Bar')
assert module.Bar.__fields__['b'].type_ is module.Foo

Expand All @@ -81,19 +79,16 @@ class Foo(BaseModel):
foo: 'Foo'
bar: 'Bar' # noqa: F821

from pydantic.typing import ForwardRef

assert module.Foo.__fields__['bar'].type_ == ForwardRef('Bar')
assert module.Foo.__fields__['foo'].type_ is module.Foo


def test_basic_forward_ref(create_module):
@create_module
def module():
from typing import Optional
from typing import ForwardRef, Optional

from pydantic import BaseModel
from pydantic.typing import ForwardRef

class Foo(BaseModel):
a: int
Expand All @@ -110,8 +105,9 @@ class Bar(BaseModel):
def test_self_forward_ref_module(create_module):
@create_module
def module():
from typing import ForwardRef

from pydantic import BaseModel
from pydantic.typing import ForwardRef

Foo = ForwardRef('Foo')

Expand Down Expand Up @@ -166,8 +162,9 @@ class Foo(BaseModel):
def test_self_forward_ref_local(create_module):
@create_module
def module():
from typing import ForwardRef

from pydantic import BaseModel
from pydantic.typing import ForwardRef

def main():
Foo = ForwardRef('Foo')
Expand All @@ -187,8 +184,9 @@ class Foo(BaseModel):
def test_missing_update_forward_refs(create_module):
@create_module
def module():
from typing import ForwardRef

from pydantic import BaseModel
from pydantic.typing import ForwardRef

Foo = ForwardRef('Foo')

Expand Down Expand Up @@ -235,10 +233,9 @@ class Dataclass:
def test_forward_ref_sub_types(create_module):
@create_module
def module():
from typing import Union
from typing import ForwardRef, Union

from pydantic import BaseModel
from pydantic.typing import ForwardRef

class Leaf(BaseModel):
a: str
Expand All @@ -264,10 +261,9 @@ class Node(BaseModel):
def test_forward_ref_nested_sub_types(create_module):
@create_module
def module():
from typing import Tuple, Union
from typing import ForwardRef, Tuple, Union

from pydantic import BaseModel
from pydantic.typing import ForwardRef

class Leaf(BaseModel):
a: str
Expand Down Expand Up @@ -465,12 +461,11 @@ class Account(BaseModel):
def test_forward_ref_with_field(create_module):
@create_module
def module():
from typing import List
from typing import ForwardRef, List

import pytest

from pydantic import BaseModel, Field
from pydantic.typing import ForwardRef

Foo = ForwardRef('Foo')

Expand Down
3 changes: 1 addition & 2 deletions tests/test_utils.py
Expand Up @@ -5,7 +5,7 @@
import string
import sys
from copy import copy, deepcopy
from typing import Callable, Dict, List, NewType, Tuple, TypeVar, Union
from typing import Callable, Dict, ForwardRef, List, NewType, Tuple, TypeVar, Union

import pytest
from pkg_resources import safe_version
Expand All @@ -16,7 +16,6 @@
from pydantic.dataclasses import dataclass
from pydantic.fields import Undefined
from pydantic.typing import (
ForwardRef,
all_literal_values,
display_as_type,
get_args,
Expand Down

0 comments on commit 1f83221

Please sign in to comment.