Skip to content

Commit

Permalink
fixing coverage by simplifying Annotated import logic, fix #2367
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Feb 15, 2021
1 parent fc18f8e commit 50a6915
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
16 changes: 1 addition & 15 deletions pydantic/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,7 @@ def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
try:
from typing_extensions import Annotated
except ImportError:
# Create mock Annotated values distinct from `None`, which is a valid `get_origin`
# return value.
class _FalseMeta(type):
# Allow short circuiting with "Annotated[...] if Annotated else None".
def __bool__(cls):
return False

# Give a nice suggestion for unguarded use
def __getitem__(cls, key):
raise RuntimeError(
'Annotated is not supported in this python version, please `pip install typing-extensions`.'
)

class Annotated(metaclass=_FalseMeta):
pass
Annotated = None


# Annotated[...] is implemented by returning an instance of one of these classes, depending on
Expand Down
6 changes: 2 additions & 4 deletions tests/test_annotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from pydantic.fields import Undefined
from pydantic.typing import Annotated

pytestmark = pytest.mark.skipif(not Annotated, reason='typing_extensions not installed')


@pytest.mark.skipif(not Annotated, reason='typing_extensions not installed')
@pytest.mark.parametrize(
['hint_fn', 'value'],
[
Expand Down Expand Up @@ -55,7 +56,6 @@ class M(BaseModel):
assert get_type_hints(M)['x'] == hint


@pytest.mark.skipif(not Annotated, reason='typing_extensions not installed')
@pytest.mark.parametrize(
['hint_fn', 'value', 'subclass_ctx'],
[
Expand Down Expand Up @@ -93,7 +93,6 @@ class M(BaseModel):
x: hint = value


@pytest.mark.skipif(not Annotated, reason='typing_extensions not installed')
@pytest.mark.parametrize(
['hint_fn', 'value', 'empty_init_ctx'],
[
Expand Down Expand Up @@ -121,7 +120,6 @@ class M(BaseModel):
assert M().x == 5


@pytest.mark.skipif(not Annotated, reason='typing_extensions not installed')
def test_field_reuse():
field = Field(description='Long description')

Expand Down

0 comments on commit 50a6915

Please sign in to comment.