Skip to content

Commit d4df6af

Browse files
authored
Merge pull request #61 from necaris/assorted-fixes
Updates for 0.3.0
2 parents 9eb6399 + 97c61e4 commit d4df6af

File tree

7 files changed

+466
-429
lines changed

7 files changed

+466
-429
lines changed

.github/workflows/tests.yaml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,26 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99
- uses: actions/checkout@master
10-
- uses: actions/setup-python@v1
10+
- uses: actions/setup-python@v2
1111
with:
1212
python-version: 3.8
1313
- uses: pre-commit/[email protected]
1414
pytest:
1515
name: pytest
1616
runs-on: ubuntu-latest
1717
strategy:
18+
fail-fast: false
1819
matrix:
19-
python-version: [3.6, 3.7, 3.8]
20+
python-version: ["3.7", "3.8", "3.9", "3.10"]
21+
os: [ubuntu-latest, macos-latest, windows-latest]
2022
steps:
21-
- uses: actions/checkout@master
22-
- name: Install
23-
uses: abatilo/[email protected]
24-
with:
25-
python_version: ${{ matrix.python-version }}
26-
poetry_version: 1.0
27-
args: install
28-
- name: Run matrix of tests with Tox
29-
uses: abatilo/[email protected]
30-
with:
31-
python_version: ${{ matrix.python-version }}
32-
poetry_version: 1.0
33-
args: run tox
23+
- uses: actions/checkout@master
24+
- uses: actions/setup-python@v2
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
- name: Ensure poetry
28+
uses: abatilo/[email protected]
29+
- name: Install dependencies
30+
run: poetry install
31+
- name: Run matrix of tests with Tox
32+
run: poetry run tox

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repos:
1414
exclude: README.md
1515
- id: flake8
1616
- repo: https://github.com/pre-commit/mirrors-mypy
17-
rev: v0.782
17+
rev: v0.812
1818
hooks:
1919
- id: mypy
2020
args: [--ignore-missing-imports, --no-strict-optional]

graphene_pydantic/converters.py

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,28 @@
2424
from graphene.types.datetime import Date, DateTime, Time
2525
from pydantic import BaseModel
2626
from pydantic.fields import ModelField
27+
from pydantic.typing import evaluate_forwardref
2728

2829
from .registry import Registry
2930
from .util import construct_union_class_name
3031

31-
try:
32-
# Pydantic pre-1.0
33-
from pydantic.fields import Shape
34-
35-
SHAPE_SINGLETON = (Shape.SINGLETON,)
36-
SHAPE_SEQUENTIAL = (
37-
Shape.LIST,
38-
Shape.TUPLE,
39-
Shape.TUPLE_ELLIPS,
40-
Shape.SEQUENCE,
41-
Shape.SET,
42-
)
43-
SHAPE_MAPPING = (Shape.MAPPING,)
44-
except ImportError:
45-
# Pydantic 1.0+
46-
from pydantic import fields
47-
48-
SHAPE_SINGLETON = (fields.SHAPE_SINGLETON,)
49-
SHAPE_SEQUENTIAL = (
50-
fields.SHAPE_LIST,
51-
fields.SHAPE_TUPLE,
52-
fields.SHAPE_TUPLE_ELLIPSIS,
53-
fields.SHAPE_SEQUENCE,
54-
fields.SHAPE_SET,
32+
from pydantic import fields
33+
34+
SHAPE_SINGLETON = (fields.SHAPE_SINGLETON,)
35+
SHAPE_SEQUENTIAL = (
36+
fields.SHAPE_LIST,
37+
fields.SHAPE_TUPLE,
38+
fields.SHAPE_TUPLE_ELLIPSIS,
39+
fields.SHAPE_SEQUENCE,
40+
fields.SHAPE_SET,
41+
)
42+
43+
if hasattr(fields, "SHAPE_DICT"):
44+
SHAPE_MAPPING = T.cast(
45+
T.Tuple, (fields.SHAPE_MAPPING, fields.SHAPE_DICT, fields.SHAPE_DEFAULTDICT)
5546
)
56-
SHAPE_MAPPING = (fields.SHAPE_MAPPING,)
47+
else:
48+
SHAPE_MAPPING = T.cast(T.Tuple, (fields.SHAPE_MAPPING,))
5749

5850

5951
try:
@@ -139,6 +131,10 @@ def convert_pydantic_field(
139131
# - maybe even (Sphinx-style) parse attribute documentation
140132
field_kwargs.setdefault("description", field.field_info.description)
141133

134+
# Somehow, this happens
135+
if "type_" not in field_kwargs and "type" in field_kwargs:
136+
field_kwargs["type_"] = field_kwargs.pop("type")
137+
142138
return Field(resolver=get_attr_resolver(field.name), **field_kwargs)
143139

144140

@@ -228,7 +224,7 @@ def find_graphene_type(
228224
"See the README for more on forward references."
229225
)
230226
module_ns = sys.modules[sibling.__module__].__dict__
231-
resolved = type_._evaluate(module_ns, None)
227+
resolved = evaluate_forwardref(type_, module_ns, None)
232228
# TODO: make this behavior optional. maybe this is a place for the TypeOptions to play a role?
233229
if registry:
234230
registry.add_placeholder_for_model(resolved)
@@ -267,7 +263,7 @@ def convert_generic_python_type(
267263
return convert_union_type(
268264
type_, field, registry, parent_type=parent_type, model=model
269265
)
270-
elif origin == T.Literal:
266+
elif hasattr(T, "Literal") and origin == T.Literal:
271267
return convert_literal_type(
272268
type_, field, registry, parent_type=parent_type, model=model
273269
)
@@ -338,6 +334,7 @@ def convert_union_type(
338334
)
339335
return union_cls
340336

337+
341338
def convert_literal_type(
342339
type_: T.Type,
343340
field: ModelField,
@@ -351,11 +348,7 @@ def convert_literal_type(
351348
inner_types = type_.__args__
352349
# Here we'll expand the subtypes of this Literal into a corresponding more
353350
# general scalar type.
354-
scalar_types = {
355-
type(x)
356-
for x in inner_types
357-
if x != NONE_TYPE
358-
}
351+
scalar_types = {type(x) for x in inner_types if x != NONE_TYPE}
359352
graphene_scalar_types = [
360353
convert_pydantic_type(x, field, registry, parent_type=parent_type, model=model)
361354
for x in scalar_types
@@ -368,6 +361,10 @@ def convert_literal_type(
368361
internal_meta_cls = type("Meta", (), {"types": graphene_scalar_types})
369362

370363
union_cls = type(
371-
construct_union_class_name(scalar_types), (Union,), {"Meta": internal_meta_cls}
364+
construct_union_class_name(
365+
sorted(scalar_types, key=lambda x: x.__class__.__name__)
366+
),
367+
(Union,),
368+
{"Meta": internal_meta_cls},
372369
)
373370
return union_cls

0 commit comments

Comments
 (0)