Add Pyupgrade (#4214)

This commit is contained in:
Hasan Ramezani
2022-07-06 08:52:01 +02:00
committed by GitHub
parent 962ea8bf21
commit 6ceff2eea2
12 changed files with 30 additions and 18 deletions
+3
View File
@@ -41,6 +41,9 @@ jobs:
- name: lint
run: make lint
- name: pyupgrade
run: make pyupgrade
- name: mypy
run: make mypy
+5
View File
@@ -17,3 +17,8 @@ repos:
entry: make mypy
types: [python]
language: system
- id: pyupgrade
name: Pyupgrade
entry: make pyupgrade
types: [python]
language: system
+5
View File
@@ -35,6 +35,7 @@ build:
.PHONY: format
format:
pyupgrade --py37-plus --exit-zero-even-if-changed `find pydantic tests -name "*.py" -type f`
$(isort)
$(black)
@@ -54,6 +55,10 @@ check-dist:
mypy:
mypy pydantic
.PHONY: pyupgrade
pyupgrade:
pyupgrade --py37-plus `find pydantic tests -name "*.py" -type f`
.PHONY: pyright
pyright:
cd tests/pyright && pyright
+5 -5
View File
@@ -138,7 +138,7 @@ class Color(Representation):
True - always include alpha,
False - always omit alpha,
"""
r, g, b = [float_to_255(c) for c in self._rgba[:3]]
r, g, b = (float_to_255(c) for c in self._rgba[:3])
if alpha is None:
if self._rgba.alpha is None:
return r, g, b
@@ -204,10 +204,10 @@ def parse_tuple(value: Tuple[Any, ...]) -> RGBA:
Parse a tuple or list as a color.
"""
if len(value) == 3:
r, g, b = [parse_color_value(v) for v in value]
r, g, b = (parse_color_value(v) for v in value)
return RGBA(r, g, b, None)
elif len(value) == 4:
r, g, b = [parse_color_value(v) for v in value[:3]]
r, g, b = (parse_color_value(v) for v in value[:3])
return RGBA(r, g, b, parse_float_alpha(value[3]))
else:
raise ColorError(reason='tuples must have length 3 or 4')
@@ -233,7 +233,7 @@ def parse_str(value: str) -> RGBA:
m = re.fullmatch(r_hex_short, value_lower)
if m:
*rgb, a = m.groups()
r, g, b = [int(v * 2, 16) for v in rgb]
r, g, b = (int(v * 2, 16) for v in rgb)
if a:
alpha: Optional[float] = int(a * 2, 16) / 255
else:
@@ -243,7 +243,7 @@ def parse_str(value: str) -> RGBA:
m = re.fullmatch(r_hex_long, value_lower)
if m:
*rgb, a = m.groups()
r, g, b = [int(v, 16) for v in rgb]
r, g, b = (int(v, 16) for v in rgb)
if a:
alpha = int(a, 16) / 255
else:
+1 -1
View File
@@ -181,7 +181,7 @@ class GenericModel(BaseModel):
def build_base_model(
base_model: Type[GenericModel], mapped_types: Parametrization
) -> Iterator[Type[GenericModel]]:
base_parameters = tuple([mapped_types[param] for param in base_model.__parameters__])
base_parameters = tuple(mapped_types[param] for param in base_model.__parameters__)
parameterized_base = base_model.__class_getitem__(base_parameters)
if parameterized_base is base_model or parameterized_base is cls:
# Avoid duplication in MRO
+1 -1
View File
@@ -309,7 +309,7 @@ class PydanticModelTransformer:
known_fields.add(name)
superclass_fields.append(field)
else:
(field,) = [a for a in all_fields if a.name == name]
(field,) = (a for a in all_fields if a.name == name)
all_fields.remove(field)
superclass_fields.append(field)
all_fields = superclass_fields + all_fields
+1
View File
@@ -3,6 +3,7 @@ flake8==4.0.1
flake8-quotes==3.3.1
hypothesis==6.48.2
isort==5.10.1
pyupgrade==2.34.0
mypy==0.950
pre-commit==2.19.0
pycodestyle==2.8.0
+1 -1
View File
@@ -205,7 +205,7 @@ def test_post_init_post_parse():
def test_post_init_post_parse_types():
@pydantic.dataclasses.dataclass
class CustomType(object):
class CustomType:
b: int
@pydantic.dataclasses.dataclass
+1 -1
View File
@@ -73,7 +73,7 @@ def test_path_encoding(tmpdir):
dir_path = tmpdir / 'baz'
dir_path.mkdir()
model = PathModel(path=Path('/path/test/example/'), file_path=file_path, dir_path=dir_path)
expected = '{{"path": "/path/test/example", "file_path": "{}", "dir_path": "{}"}}'.format(file_path, dir_path)
expected = f'{{"path": "/path/test/example", "file_path": "{file_path}", "dir_path": "{dir_path}"}}'
assert json.dumps(model, default=pydantic_encoder) == expected
+1 -2
View File
@@ -274,8 +274,7 @@ def test_custom_getter_dict_derived_model_class():
__custom__ = True
def __iter__(self):
for elem in range(5):
yield elem
yield from range(5)
class Example:
def __init__(self, *args, **kwargs):
+3 -3
View File
@@ -1134,7 +1134,7 @@ def test_flat_models_unique_models():
from pydantic_schema_test.moduled.modeld import Model as ModelD
flat_models = get_flat_models_from_models([ModelA, ModelB, ModelD])
assert flat_models == set([ModelA, ModelB])
assert flat_models == {ModelA, ModelB}
def test_flat_models_with_submodels():
@@ -1148,7 +1148,7 @@ def test_flat_models_with_submodels():
c: Dict[str, Bar]
flat_models = get_flat_models_from_model(Baz)
assert flat_models == set([Foo, Bar, Baz])
assert flat_models == {Foo, Bar, Baz}
def test_flat_models_with_submodels_from_sequence():
@@ -1166,7 +1166,7 @@ def test_flat_models_with_submodels_from_sequence():
ingredients: List[Ingredient]
flat_models = get_flat_models_from_models([Bar, Pizza])
assert flat_models == set([Foo, Bar, Ingredient, Pizza])
assert flat_models == {Foo, Bar, Ingredient, Pizza}
def test_model_name_maps():
+3 -4
View File
@@ -418,7 +418,7 @@ def test_constrained_set_too_long():
v: conset(int, max_items=10) = []
with pytest.raises(ValidationError) as exc_info:
ConSetModelMax(v=set(str(i) for i in range(11)))
ConSetModelMax(v={str(i) for i in range(11)})
assert exc_info.value.errors() == [
{
'loc': ('v',),
@@ -1390,8 +1390,7 @@ def test_infinite_iterable_validate_first():
def str_iterable():
while True:
for c in 'foobarbaz':
yield c
yield from 'foobarbaz'
with pytest.raises(ValidationError) as exc_info:
Model(it=str_iterable(), b=3)
@@ -1618,7 +1617,7 @@ def test_strict_bytes_subclass():
b = Model(v=MyStrictBytes(bytearray('foobar', 'utf-8')))
assert isinstance(b.v, MyStrictBytes)
assert b.v == 'foobar'.encode()
assert b.v == b'foobar'
def test_strict_str():