Skip to content

Commit

Permalink
Merge pull request #101 from dsgibbons/chore/remove-up007
Browse files Browse the repository at this point in the history
chore: turn off UP007
  • Loading branch information
thomasaarholt authored Oct 22, 2024
2 parents 53d3777 + 3dd17a2 commit 61be2d2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ extend-exclude= ["tests/__init__.py"]

[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "I", "B", "D", "UP"]
ignore = ["UP007"]

[tool.ruff.lint.pydocstyle]
convention = "google"
10 changes: 5 additions & 5 deletions src/patito/_pydantic/column_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ class ColumnInfo(BaseModel, arbitrary_types_allowed=True):
"""

allow_missing: Optional[bool] = None # noqa: UP007
allow_missing: Optional[bool] = None
dtype: Annotated[
Optional[Union[DataTypeClass, DataType]], # noqa: UP007
Optional[Union[DataTypeClass, DataType]],
BeforeValidator(dtype_deserializer),
] = None
constraints: Annotated[
Optional[Union[pl.Expr, list[pl.Expr]]], # noqa: UP007
Optional[Union[pl.Expr, list[pl.Expr]]],
BeforeValidator(expr_deserializer),
] = None
derived_from: Annotated[
Optional[Union[str, pl.Expr]], # noqa: UP007
Optional[Union[str, pl.Expr]],
BeforeValidator(expr_or_col_name_deserializer),
] = None
unique: Optional[bool] = None # noqa : UP007
unique: Optional[bool] = None

def __repr__(self) -> str:
"""Print only Field attributes whose values are not default (mainly None)."""
Expand Down
20 changes: 10 additions & 10 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ def test_model_joins() -> None:

class Left(pt.Model):
left: int = pt.Field(gt=20)
opt_left: Optional[int] = None # noqa: UP007
opt_left: Optional[int] = None

class Right(pt.Model):
right: int = pt.Field(gt=20)
opt_right: Optional[int] = None # noqa: UP007
opt_right: Optional[int] = None

def test_model_validator(model: type[pt.Model]) -> None:
"""Test if all field validators have been included correctly."""
Expand Down Expand Up @@ -295,7 +295,7 @@ def test_model_selects() -> None:
"""It should produce models compatible with select statements."""

class MyModel(pt.Model):
a: Optional[int] # noqa: UP007
a: Optional[int]
b: int = pt.Field(gt=10)

MySubModel = MyModel.select("b")
Expand All @@ -321,7 +321,7 @@ def test_model_prefix_and_suffix() -> None:
"""It should produce models where all fields have been prefixed/suffixed."""

class MyModel(pt.Model):
a: Optional[int] # noqa: UP007
a: Optional[int]
b: str

NewModel = MyModel.prefix("pre_").suffix("_post")
Expand All @@ -333,7 +333,7 @@ def test_model_field_renaming() -> None:
"""It should be able to change its field names."""

class MyModel(pt.Model):
a: Optional[int] # noqa: UP007
a: Optional[int]
b: str

NewModel = MyModel.rename({"b": "B"})
Expand Down Expand Up @@ -425,7 +425,7 @@ def validate_model_schema(schema) -> None:
class ParentModel(pt.Model):
a: int
b: Model
c: Optional[float] = None # noqa: UP007
c: Optional[float] = None

schema = ParentModel.model_schema
validate_model_schema(
Expand All @@ -445,13 +445,13 @@ def test_nullable_columns() -> None:
"""Ensure columns are correctly nullable."""

class Test1(pt.Model):
foo: Optional[str] = pt.Field(dtype=pl.String) # noqa: UP007
foo: Optional[str] = pt.Field(dtype=pl.String)

assert Test1.nullable_columns == {"foo"}
assert set(Test1.valid_dtypes["foo"]) == {pl.String}

class Test2(pt.Model):
foo: Optional[int] = pt.Field(dtype=pl.UInt32) # noqa: UP007
foo: Optional[int] = pt.Field(dtype=pl.UInt32)

assert Test2.nullable_columns == {"foo"}
assert set(Test2.valid_dtypes["foo"]) == {pl.UInt32}
Expand All @@ -476,7 +476,7 @@ class Test2(pt.Model):
with pytest.raises(ValueError, match="Invalid dtype UInt32"):

class Test3(pt.Model):
foo: Optional[str] = pt.Field(dtype=pl.UInt32) # noqa: UP007
foo: Optional[str] = pt.Field(dtype=pl.UInt32)

Test3.validate_schema()

Expand Down Expand Up @@ -536,7 +536,7 @@ class SubModel(pt.Model):
class Test(pt.Model):
a: int
b: int
c: Optional[SubModel] # noqa: UP007
c: Optional[SubModel]

df = Test.examples({"a": range(5), "c": None})
Test.validate(df.cast())
Expand Down
38 changes: 18 additions & 20 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def test_is_optional() -> None:
)
def test_is_optional_with_pipe_operator() -> None:
"""It should return True for optional types."""
assert is_optional(Optional[int]) # noqa: UP007
assert is_optional(Optional[int])


def test_dewrap_optional() -> None:
"""It should return the inner type of Optional types."""
assert unwrap_optional(Optional[int]) is int # noqa: UP007
assert unwrap_optional(Optional[int]) is int
assert unwrap_optional(Union[int, None]) is int
assert unwrap_optional(int) is int

Expand All @@ -53,9 +53,7 @@ def test_dewrap_optional() -> None:
)
def test_dewrap_optional_with_pipe_operator() -> None:
"""It should return the inner type of Optional types."""
assert (
unwrap_optional(Optional[int]) is int # noqa: UP007
)
assert unwrap_optional(Optional[int]) is int


def test_validation_returns_df() -> None:
Expand Down Expand Up @@ -154,7 +152,7 @@ class OuterModel(pt.Model):
OuterModel.validate(df_missing_nested_column_2)

class OuterModelWithOptionalInner(pt.Model):
inner: Optional[InnerModel] # noqa: UP007
inner: Optional[InnerModel]
other: str

df_missing_nested_column_2 = pl.DataFrame(
Expand All @@ -180,7 +178,7 @@ class OuterModelWithListInner(pt.Model):
OuterModelWithListInner.validate(df_missing_nested_column_2)

class OuterModelWithOptionalListInner(pt.Model):
inner: Optional[list[InnerModel]] # noqa: UP007
inner: Optional[list[InnerModel]]
other: str

df_missing_nested_column_2 = pl.DataFrame(
Expand All @@ -192,7 +190,7 @@ class OuterModelWithOptionalListInner(pt.Model):
OuterModelWithOptionalListInner.validate(df_missing_nested_column_2)

class OuterModelWithListOptionalInner(pt.Model):
inner: list[Optional[InnerModel]] # noqa: UP007
inner: list[Optional[InnerModel]]
other: str

df_missing_nested_column_2 = pl.DataFrame(
Expand All @@ -207,7 +205,7 @@ class OuterModelWithListOptionalInner(pt.Model):
OuterModelWithListOptionalInner.validate(df_missing_nested_column_2)

class OuterModelWithOptionalListOptionalInner(pt.Model):
inner: Optional[list[Optional[InnerModel]]] # noqa: UP007
inner: Optional[list[Optional[InnerModel]]]
other: str

df_missing_nested_column_2 = pl.DataFrame(
Expand Down Expand Up @@ -280,7 +278,7 @@ def test_validate_non_nullable_columns() -> None:

class SmallModel(pt.Model):
column_1: int
column_2: Optional[int] = None # noqa: UP007
column_2: Optional[int] = None

# We insert nulls into a non-optional column, causing an exception
wrong_nulls_df = pl.DataFrame().with_columns(
Expand Down Expand Up @@ -500,7 +498,7 @@ class ABCEnum(enum.Enum):
THREE = "c"

class EnumModel(pt.Model):
column: Optional[ABCEnum] # noqa: UP007
column: Optional[ABCEnum]

valid_df = pl.DataFrame({"column": ["a", "b", "b", "c"]})
validate(dataframe=valid_df, schema=EnumModel)
Expand Down Expand Up @@ -558,7 +556,7 @@ def test_optional_literal_enum_validation() -> None:
"""Test validation of optional typing.Literal-typed fields."""

class EnumModel(pt.Model):
column: Optional[Literal["a", "b", "c"]] # noqa: UP007
column: Optional[Literal["a", "b", "c"]]

valid_df = pl.DataFrame({"column": ["a", "b", "b", "c"]})
validate(dataframe=valid_df, schema=EnumModel)
Expand Down Expand Up @@ -939,7 +937,7 @@ def test_optional_enum() -> None:

class OptionalEnumModel(pt.Model):
# Old type annotation syntax
optional_enum: Optional[Literal["A", "B"]] # noqa: UP007
optional_enum: Optional[Literal["A", "B"]]

df = pl.DataFrame({"optional_enum": ["A", "B", None]})
OptionalEnumModel.validate(df)
Expand All @@ -954,9 +952,9 @@ def test_optional_pipe_operator() -> None:

class OptionalEnumModel(pt.Model):
# Old type annotation syntax
optional_enum_1: Optional[Literal["A", "B"]] # noqa: UP007
optional_enum_1: Optional[Literal["A", "B"]]
# New type annotation syntax
optional_enum_2: Optional[Literal["A", "B"]] # noqa: UP007
optional_enum_2: Optional[Literal["A", "B"]]

df = pl.DataFrame(
{
Expand All @@ -978,9 +976,9 @@ def test_validation_of_list_dtypes() -> None:

class ListModel(pt.Model):
int_list: list[int]
int_or_null_list: list[Optional[int]] # noqa: UP007
nullable_int_list: Optional[list[int]] # noqa: UP007
nullable_int_or_null_list: Optional[list[Optional[int]]] # noqa: UP007
int_or_null_list: list[Optional[int]]
nullable_int_list: Optional[list[int]]
nullable_int_or_null_list: Optional[list[Optional[int]]]

valid_df = pl.DataFrame(
{
Expand Down Expand Up @@ -1020,7 +1018,7 @@ class Outer(pt.Model):
id: str
code: str
label: str
inner_types: Optional[list[Inner]] # noqa: UP007
inner_types: Optional[list[Inner]]

good_df = pl.DataFrame(
{
Expand Down Expand Up @@ -1058,7 +1056,7 @@ def test_nested_field_attrs() -> None:
"""Ensure that constraints are respected even when embedded inside 'anyOf'."""

class Test(pt.Model):
foo: Optional[int] = pt.Field( # noqa: UP007
foo: Optional[int] = pt.Field(
dtype=pl.Int64, ge=0, le=100, constraints=pt.field.sum() == 100
)

Expand Down

0 comments on commit 61be2d2

Please sign in to comment.