Skip to content

Commit

Permalink
(feat, python): use ruff for formatting (#4199)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Aug 5, 2024
1 parent 8d35d89 commit be092b3
Show file tree
Hide file tree
Showing 4,786 changed files with 86,293 additions and 32,909 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
33 changes: 26 additions & 7 deletions generators/python/core_utilities/fastapi/pydantic_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,27 @@ class Config:
json_encoders = {dt.datetime: serialize_datetime}

def json(self, **kwargs: typing.Any) -> str:
kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
kwargs_with_defaults: typing.Any = {
"by_alias": True,
"exclude_unset": True,
**kwargs,
}
if IS_PYDANTIC_V2:
return super().model_dump_json(**kwargs_with_defaults) # type: ignore # Pydantic v2
else:
return super().json(**kwargs_with_defaults)

def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
kwargs_with_defaults_exclude_unset: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
kwargs_with_defaults_exclude_none: typing.Any = {"by_alias": True, "exclude_none": True, **kwargs}
kwargs_with_defaults_exclude_unset: typing.Any = {
"by_alias": True,
"exclude_unset": True,
**kwargs,
}
kwargs_with_defaults_exclude_none: typing.Any = {
"by_alias": True,
"exclude_none": True,
**kwargs,
}

if IS_PYDANTIC_V2:
return deep_union_pydantic_dicts(
Expand All @@ -109,7 +121,8 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
)
else:
return deep_union_pydantic_dicts(
super().dict(**kwargs_with_defaults_exclude_unset), super().dict(**kwargs_with_defaults_exclude_none)
super().dict(**kwargs_with_defaults_exclude_unset),
super().dict(**kwargs_with_defaults_exclude_none),
)


Expand Down Expand Up @@ -149,12 +162,16 @@ def update_forward_refs(model: typing.Type["Model"], **localns: typing.Any) -> N
AnyCallable = typing.Callable[..., typing.Any]


def universal_root_validator(pre: bool = False) -> typing.Callable[[AnyCallable], AnyCallable]:
def universal_root_validator(
pre: bool = False,
) -> typing.Callable[[AnyCallable], AnyCallable]:
def decorator(func: AnyCallable) -> AnyCallable:
@wraps(func)
def validate(*args: typing.Any, **kwargs: typing.Any) -> AnyCallable:
if IS_PYDANTIC_V2:
wrapped_func = pydantic.model_validator("before" if pre else "after")(func) # type: ignore # Pydantic v2
wrapped_func = pydantic.model_validator("before" if pre else "after")(
func
) # type: ignore # Pydantic v2
else:
wrapped_func = pydantic.root_validator(pre=pre)(func) # type: ignore # Pydantic v1

Expand All @@ -170,7 +187,9 @@ def decorator(func: AnyCallable) -> AnyCallable:
@wraps(func)
def validate(*args: typing.Any, **kwargs: typing.Any) -> AnyCallable:
if IS_PYDANTIC_V2:
wrapped_func = pydantic.field_validator(field_name, mode="before" if pre else "after")(func) # type: ignore # Pydantic v2
wrapped_func = pydantic.field_validator(field_name, mode="before" if pre else "after")(
func
) # type: ignore # Pydantic v2
else:
wrapped_func = pydantic.validator(field_name, pre=pre)(func)

Expand Down
6 changes: 5 additions & 1 deletion generators/python/core_utilities/fastapi/route_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ def decorator(endpoint_function: T) -> T:
setattr(
endpoint_function,
FERN_CONFIG_KEY,
RouteArgs(openapi_extra=openapi_extra, tags=tags, include_in_schema=include_in_schema),
RouteArgs(
openapi_extra=openapi_extra,
tags=tags,
include_in_schema=include_in_schema,
),
)
return endpoint_function

Expand Down
33 changes: 26 additions & 7 deletions generators/python/core_utilities/pydantic/pydantic_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,27 @@ class Config:
json_encoders = {dt.datetime: serialize_datetime}

def json(self, **kwargs: typing.Any) -> str:
kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
kwargs_with_defaults: typing.Any = {
"by_alias": True,
"exclude_unset": True,
**kwargs,
}
if IS_PYDANTIC_V2:
return super().model_dump_json(**kwargs_with_defaults) # type: ignore # Pydantic v2
else:
return super().json(**kwargs_with_defaults)

def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
kwargs_with_defaults_exclude_unset: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
kwargs_with_defaults_exclude_none: typing.Any = {"by_alias": True, "exclude_none": True, **kwargs}
kwargs_with_defaults_exclude_unset: typing.Any = {
"by_alias": True,
"exclude_unset": True,
**kwargs,
}
kwargs_with_defaults_exclude_none: typing.Any = {
"by_alias": True,
"exclude_none": True,
**kwargs,
}

if IS_PYDANTIC_V2:
return deep_union_pydantic_dicts(
Expand All @@ -109,7 +121,8 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
)
else:
return deep_union_pydantic_dicts(
super().dict(**kwargs_with_defaults_exclude_unset), super().dict(**kwargs_with_defaults_exclude_none)
super().dict(**kwargs_with_defaults_exclude_unset),
super().dict(**kwargs_with_defaults_exclude_none),
)


Expand Down Expand Up @@ -149,12 +162,16 @@ def update_forward_refs(model: typing.Type["Model"], **localns: typing.Any) -> N
AnyCallable = typing.Callable[..., typing.Any]


def universal_root_validator(pre: bool = False) -> typing.Callable[[AnyCallable], AnyCallable]:
def universal_root_validator(
pre: bool = False,
) -> typing.Callable[[AnyCallable], AnyCallable]:
def decorator(func: AnyCallable) -> AnyCallable:
@wraps(func)
def validate(*args: typing.Any, **kwargs: typing.Any) -> AnyCallable:
if IS_PYDANTIC_V2:
wrapped_func = pydantic.model_validator("before" if pre else "after")(func) # type: ignore # Pydantic v2
wrapped_func = pydantic.model_validator("before" if pre else "after")(
func
) # type: ignore # Pydantic v2
else:
wrapped_func = pydantic.root_validator(pre=pre)(func) # type: ignore # Pydantic v1

Expand All @@ -170,7 +187,9 @@ def decorator(func: AnyCallable) -> AnyCallable:
@wraps(func)
def validate(*args: typing.Any, **kwargs: typing.Any) -> AnyCallable:
if IS_PYDANTIC_V2:
wrapped_func = pydantic.field_validator(field_name, mode="before" if pre else "after")(func) # type: ignore # Pydantic v2
wrapped_func = pydantic.field_validator(field_name, mode="before" if pre else "after")(
func
) # type: ignore # Pydantic v2
else:
wrapped_func = pydantic.validator(field_name, pre=pre)(func)

Expand Down
5 changes: 4 additions & 1 deletion generators/python/core_utilities/pydantic/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def __init__(self, *, alias: str) -> None:


def convert_and_respect_annotation_metadata(
*, object_: typing.Any, annotation: typing.Any, inner_type: typing.Optional[typing.Any] = None
*,
object_: typing.Any,
annotation: typing.Any,
inner_type: typing.Optional[typing.Any] = None,
) -> typing.Any:
"""
Respect the metadata annotations on a field, such as aliasing. This function effectively
Expand Down
17 changes: 13 additions & 4 deletions generators/python/core_utilities/pydantic/unchecked_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ def __init__(self, *, discriminant: str) -> None:

class UncheckedBaseModel(UniversalBaseModel):
if IS_PYDANTIC_V2:
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow") # type: ignore # Pydantic v2
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(
extra="allow"
) # type: ignore # Pydantic v2
else:

class Config:
extra = pydantic.Extra.allow

@classmethod
def model_construct(
cls: typing.Type["Model"], _fields_set: typing.Optional[typing.Set[str]] = None, **values: typing.Any
cls: typing.Type["Model"],
_fields_set: typing.Optional[typing.Set[str]] = None,
**values: typing.Any,
) -> "Model":
# Fallback construct function to the specified override below.
return cls.construct(_fields_set=_fields_set, **values)
Expand All @@ -51,7 +55,9 @@ def model_construct(
# Implementation taken from: https://github.com/pydantic/pydantic/issues/1168#issuecomment-817742836
@classmethod
def construct(
cls: typing.Type["Model"], _fields_set: typing.Optional[typing.Set[str]] = None, **values: typing.Any
cls: typing.Type["Model"],
_fields_set: typing.Optional[typing.Set[str]] = None,
**values: typing.Any,
) -> "Model":
m = cls.__new__(cls)
fields_values = {}
Expand Down Expand Up @@ -259,9 +265,12 @@ def _get_is_populate_by_name(model: typing.Type["Model"]) -> bool:

PydanticField = typing.Union[ModelField, pydantic.fields.FieldInfo]


# Pydantic V1 swapped the typing of __fields__'s values from ModelField to FieldInfo
# And so we try to handle both V1 cases, as well as V2 (FieldInfo from model.model_fields)
def _get_model_fields(model: typing.Type["Model"]) -> typing.Mapping[str, PydanticField]:
def _get_model_fields(
model: typing.Type["Model"],
) -> typing.Mapping[str, PydanticField]:
if IS_PYDANTIC_V2:
return model.model_fields # type: ignore # Pydantic v2
else:
Expand Down
39 changes: 31 additions & 8 deletions generators/python/core_utilities/sdk/base_test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def validate_field(response: typing.Any, json_expectation: typing.Any, type_expe
if isinstance(entry_expectation, dict):
is_container_of_complex_type = True
validate_response(
response=response[idx], json_expectation=ex, type_expectations=entry_expectation
response=response[idx],
json_expectation=ex,
type_expectations=entry_expectation,
)
else:
cast_json_expectation.append(cast_field(ex, entry_expectation))
Expand All @@ -55,7 +57,10 @@ def validate_field(response: typing.Any, json_expectation: typing.Any, type_expe
# if any of the values of the set have a type_expectation of a dict, we're assuming it's a pydantic
# model and keeping it a list.
if container_expectation != "set" or not any(
map(lambda value: isinstance(value, dict), list(contents_expectation.values()))
map(
lambda value: isinstance(value, dict),
list(contents_expectation.values()),
)
):
json_expectation = cast_field(json_expectation, container_expectation)
elif isinstance(type_expectation, tuple):
Expand All @@ -64,9 +69,15 @@ def validate_field(response: typing.Any, json_expectation: typing.Any, type_expe
if isinstance(contents_expectation, dict):
json_expectation = {
cast_field(
key, contents_expectation.get(idx)[0] if contents_expectation.get(idx) is not None else None # type: ignore
key,
contents_expectation.get(idx)[0]
if contents_expectation.get(idx) is not None
else None, # type: ignore
): cast_field(
value, contents_expectation.get(idx)[1] if contents_expectation.get(idx) is not None else None # type: ignore
value,
contents_expectation.get(idx)[1]
if contents_expectation.get(idx) is not None
else None, # type: ignore
)
for idx, (key, value) in enumerate(json_expectation.items())
}
Expand Down Expand Up @@ -95,7 +106,11 @@ def validate_response(response: typing.Any, json_expectation: typing.Any, type_e
and not isinstance(response, dict)
and not issubclass(type(response), pydantic.BaseModel)
):
validate_field(response=response, json_expectation=json_expectation, type_expectation=type_expectations)
validate_field(
response=response,
json_expectation=json_expectation,
type_expectation=type_expectations,
)
return

if isinstance(response, list):
Expand All @@ -107,7 +122,9 @@ def validate_response(response: typing.Any, json_expectation: typing.Any, type_e
content_expectation = type_expectations[1]
for idx, item in enumerate(response):
validate_response(
response=item, json_expectation=json_expectation[idx], type_expectations=content_expectation[idx]
response=item,
json_expectation=json_expectation[idx],
type_expectations=content_expectation[idx],
)
else:
response_json = response
Expand All @@ -127,10 +144,16 @@ def validate_response(response: typing.Any, json_expectation: typing.Any, type_e
# Otherwise, we're just validating a single field that's a pydantic model.
if isinstance(value, dict) and not isinstance(type_expectation, tuple):
validate_response(
response=response_json[key], json_expectation=value, type_expectations=type_expectation
response=response_json[key],
json_expectation=value,
type_expectations=type_expectation,
)
else:
validate_field(response=response_json[key], json_expectation=value, type_expectation=type_expectation)
validate_field(
response=response_json[key],
json_expectation=value,
type_expectation=type_expectation,
)

# Ensure there are no additional fields here either
del response_json[key]
Expand Down
9 changes: 7 additions & 2 deletions generators/python/core_utilities/sdk/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
# (filename, file (or bytes), content_type)
typing.Tuple[typing.Optional[str], FileContent, typing.Optional[str]],
# (filename, file (or bytes), content_type, headers)
typing.Tuple[typing.Optional[str], FileContent, typing.Optional[str], typing.Mapping[str, str]],
typing.Tuple[
typing.Optional[str],
FileContent,
typing.Optional[str],
typing.Mapping[str, str],
],
]


def convert_file_dict_to_httpx_tuples(
d: typing.Dict[str, typing.Union[File, typing.List[File]]]
d: typing.Dict[str, typing.Union[File, typing.List[File]]],
) -> typing.List[typing.Tuple[str, File]]:
"""
The format we use is a list of tuples, where the first element is the
Expand Down
3 changes: 2 additions & 1 deletion generators/python/core_utilities/sdk/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def _should_retry(response: httpx.Response) -> bool:


def remove_omit_from_dict(
original: typing.Dict[str, typing.Optional[typing.Any]], omit: typing.Optional[typing.Any]
original: typing.Dict[str, typing.Optional[typing.Any]],
omit: typing.Optional[typing.Any],
) -> typing.Dict[str, typing.Any]:
if omit is None:
return original
Expand Down
33 changes: 26 additions & 7 deletions generators/python/core_utilities/sdk/pydantic_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,27 @@ class Config:
json_encoders = {dt.datetime: serialize_datetime}

def json(self, **kwargs: typing.Any) -> str:
kwargs_with_defaults: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
kwargs_with_defaults: typing.Any = {
"by_alias": True,
"exclude_unset": True,
**kwargs,
}
if IS_PYDANTIC_V2:
return super().model_dump_json(**kwargs_with_defaults) # type: ignore # Pydantic v2
else:
return super().json(**kwargs_with_defaults)

def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
kwargs_with_defaults_exclude_unset: typing.Any = {"by_alias": True, "exclude_unset": True, **kwargs}
kwargs_with_defaults_exclude_none: typing.Any = {"by_alias": True, "exclude_none": True, **kwargs}
kwargs_with_defaults_exclude_unset: typing.Any = {
"by_alias": True,
"exclude_unset": True,
**kwargs,
}
kwargs_with_defaults_exclude_none: typing.Any = {
"by_alias": True,
"exclude_none": True,
**kwargs,
}

if IS_PYDANTIC_V2:
return deep_union_pydantic_dicts(
Expand All @@ -109,7 +121,8 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
)
else:
return deep_union_pydantic_dicts(
super().dict(**kwargs_with_defaults_exclude_unset), super().dict(**kwargs_with_defaults_exclude_none)
super().dict(**kwargs_with_defaults_exclude_unset),
super().dict(**kwargs_with_defaults_exclude_none),
)


Expand Down Expand Up @@ -149,12 +162,16 @@ def update_forward_refs(model: typing.Type["Model"], **localns: typing.Any) -> N
AnyCallable = typing.Callable[..., typing.Any]


def universal_root_validator(pre: bool = False) -> typing.Callable[[AnyCallable], AnyCallable]:
def universal_root_validator(
pre: bool = False,
) -> typing.Callable[[AnyCallable], AnyCallable]:
def decorator(func: AnyCallable) -> AnyCallable:
@wraps(func)
def validate(*args: typing.Any, **kwargs: typing.Any) -> AnyCallable:
if IS_PYDANTIC_V2:
wrapped_func = pydantic.model_validator("before" if pre else "after")(func) # type: ignore # Pydantic v2
wrapped_func = pydantic.model_validator("before" if pre else "after")(
func
) # type: ignore # Pydantic v2
else:
wrapped_func = pydantic.root_validator(pre=pre)(func) # type: ignore # Pydantic v1

Expand All @@ -170,7 +187,9 @@ def decorator(func: AnyCallable) -> AnyCallable:
@wraps(func)
def validate(*args: typing.Any, **kwargs: typing.Any) -> AnyCallable:
if IS_PYDANTIC_V2:
wrapped_func = pydantic.field_validator(field_name, mode="before" if pre else "after")(func) # type: ignore # Pydantic v2
wrapped_func = pydantic.field_validator(field_name, mode="before" if pre else "after")(
func
) # type: ignore # Pydantic v2
else:
wrapped_func = pydantic.validator(field_name, pre=pre)(func) # type: ignore # Pydantic v1

Expand Down
Loading

0 comments on commit be092b3

Please sign in to comment.