Skip to content

Commit 8c4ef97

Browse files
committed
lint
1 parent d9becd0 commit 8c4ef97

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

openapi_python_client/parser/properties/merge_properties.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from collections.abc import Callable
32
from typing import Any
43

@@ -15,9 +14,9 @@
1514

1615
def merge_properties(prop1: PropertyProtocol, prop2: PropertyProtocol) -> PropertyProtocol:
1716
"""Attempt to create a new property that incorporates the behavior of both.
18-
17+
1918
This is used when merging schemas with allOf, when two schemas define a property with the same name.
20-
19+
2120
OpenAPI defines allOf in terms of validation behavior: the input must pass the validation rules
2221
defined in all of the listed schemas. Our task here is slightly more difficult, since we must end
2322
up with a single Property object that will be used to generate a single class property in the
@@ -37,10 +36,10 @@ def merge_properties(prop1: PropertyProtocol, prop2: PropertyProtocol) -> Proper
3736

3837
if prop1.__class__ == prop2.__class__:
3938
return _merge_same_type(prop1, prop2)
40-
39+
4140
if isinstance(prop1, AnyProperty) or isinstance(prop2, AnyProperty):
4241
return _merge_with_any(prop1, prop2)
43-
42+
4443
if _is_numeric(prop1) and _is_numeric(prop2):
4544
return _merge_numeric(prop1, prop2)
4645

@@ -78,13 +77,11 @@ def _merge_string(prop1: StringProperty, prop2: StringProperty) -> StringPropert
7877
# it would just mean the value must match both of the patterns to be valid. But we have no way to
7978
# represent this in our internal model currently.
8079
pattern: str | None | ValueError = _combine_values(
81-
prop1.pattern,
82-
prop2.pattern,
83-
lambda a, b: ValueError("specified two different regex patterns")
80+
prop1.pattern, prop2.pattern, lambda a, b: ValueError("specified two different regex patterns")
8481
)
8582
if isinstance(pattern, ValueError):
8683
raise pattern
87-
84+
8885
return _merge_common_attributes(evolve(prop1, max_length=max_length, pattern=pattern), prop2)
8986

9087

@@ -98,7 +95,7 @@ def _merge_numeric(prop1: PropertyProtocol, prop2: PropertyProtocol) -> IntPrope
9895
if isinstance(result.default, float) and not result.default.is_integer():
9996
raise ValueError(f"default value {result.default} is not valid for an integer property")
10097
return result
101-
98+
10299

103100
def _merge_with_any(prop1: PropertyProtocol, prop2: PropertyProtocol) -> PropertyProtocol:
104101
# AnyProperty implies no validation rules for a value, so merging it with any other type just means
@@ -123,17 +120,16 @@ def _merge_with_enum(prop1: PropertyProtocol, prop2: PropertyProtocol) -> EnumPr
123120
# If enum values were specified for just one of the properties, use those.
124121
enum_prop = prop1 if isinstance(prop1, EnumProperty) else prop2
125122
non_enum_prop = prop2 if isinstance(prop1, EnumProperty) else prop1
126-
if (
127-
(isinstance(non_enum_prop, IntProperty) and enum_prop.value_type is int) or
128-
(isinstance(non_enum_prop, StringProperty) and enum_prop.value_type is str)
123+
if (isinstance(non_enum_prop, IntProperty) and enum_prop.value_type is int) or (
124+
isinstance(non_enum_prop, StringProperty) and enum_prop.value_type is str
129125
):
130126
return _merge_common_attributes(enum_prop, prop1, prop2)
131127
raise ValueError("defined with two incompatible types")
132128

133129

134130
def _merge_common_attributes(base: PropertyProtocol, *extend_with: PropertyProtocol) -> PropertyProtocol:
135131
"""Create a new instance based on base, overriding basic attributes with values from extend_with, in order.
136-
132+
137133
For "default", "description", and "example", a non-None value overrides any value from a previously
138134
specified property. The behavior is similar to using the spread operator with dictionaries, except
139135
that None means "not specified".
@@ -146,10 +142,10 @@ def _merge_common_attributes(base: PropertyProtocol, *extend_with: PropertyProto
146142
current = evolve(
147143
current,
148144
required=current.required or override.required,
149-
default = override.default or current.default,
150-
description = override.description or current.description,
151-
example = override.example or current.example,
152-
)
145+
default=override.default or current.default,
146+
description=override.description or current.description,
147+
example=override.example or current.example,
148+
)
153149
return current
154150

155151

tests/conftest.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _factory(**kwargs):
6868
return _factory
6969

7070

71-
def _simple_factory(cls: type, default_kwargs: Union[dict, Callable[[dict], dict], None]=None):
71+
def _simple_factory(cls: type, default_kwargs: Union[dict, Callable[[dict], dict], None] = None):
7272
def _factory(**kwargs):
7373
kwargs = _common_kwargs(kwargs)
7474
defaults = default_kwargs
@@ -90,11 +90,14 @@ def enum_property_factory() -> Callable[..., EnumProperty]:
9090
"""
9191
from openapi_python_client.parser.properties import Class
9292

93-
return _simple_factory(EnumProperty, lambda kwargs: {
94-
"class_info": Class(name=kwargs["name"], module_name=kwargs["name"]),
95-
"values": {},
96-
"value_type": str,
97-
})
93+
return _simple_factory(
94+
EnumProperty,
95+
lambda kwargs: {
96+
"class_info": Class(name=kwargs["name"], module_name=kwargs["name"]),
97+
"values": {},
98+
"value_type": str,
99+
},
100+
)
98101

99102

100103
@pytest.fixture
@@ -215,9 +218,9 @@ def union_property_factory(date_time_property_factory, string_property_factory)
215218
You can pass the same params into this as the UnionProperty constructor to override defaults.
216219
"""
217220

218-
return _simple_factory(UnionProperty, {
219-
"inner_properties": [date_time_property_factory(), string_property_factory()]
220-
})
221+
return _simple_factory(
222+
UnionProperty, {"inner_properties": [date_time_property_factory(), string_property_factory()]}
223+
)
221224

222225

223226
@pytest.fixture

tests/test_parser/test_properties/test_merge_properties.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import pytest
32
from attr import evolve
43

@@ -73,9 +72,7 @@ def test_merge_int_with_float(int_property_factory, float_property_factory):
7372
assert merge_properties(int_prop, float_prop) == (
7473
evolve(int_prop, default=float_prop.default, description=float_prop.description)
7574
)
76-
assert merge_properties(float_prop, int_prop) == (
77-
evolve(int_prop, default=float_prop.default)
78-
)
75+
assert merge_properties(float_prop, int_prop) == (evolve(int_prop, default=float_prop.default))
7976

8077
float_prop_with_non_int_default = evolve(float_prop, default=2.5)
8178
with pytest.raises(ValueError) as excinfo:
@@ -151,7 +148,7 @@ def test_merge_string_with_string_enum(string_property_factory, enum_property_fa
151148
required=True,
152149
default=string_prop.default,
153150
description=string_prop.description,
154-
example=string_prop.example
151+
example=string_prop.example,
155152
)
156153

157154

@@ -168,11 +165,7 @@ def test_merge_int_with_int_enum(int_property_factory, enum_property_factory):
168165

169166
assert merge_properties(int_prop, enum_prop) == evolve(enum_prop, required=True)
170167
assert merge_properties(enum_prop, int_prop) == evolve(
171-
enum_prop,
172-
required=True,
173-
default=int_prop.default,
174-
description=int_prop.description,
175-
example=int_prop.example
168+
enum_prop, required=True, default=int_prop.default, description=int_prop.description, example=int_prop.example
176169
)
177170

178171

0 commit comments

Comments
 (0)