Skip to content

Commit

Permalink
Move to ruff for linting (#4709)
Browse files Browse the repository at this point in the history
* WIP to move to ruff for linting

* add extend-select, fix string
  • Loading branch information
samuelcolvin committed Nov 3, 2022
1 parent 5cbfcba commit a57346a
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -40,6 +40,8 @@ jobs:
with:
extra_args: --all-files --verbose

- run: make lint-flake8

- name: make history
run: python3 ./changes/make_history.py

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -36,3 +36,4 @@ pydantic/*.so
/fastapi/
/codecov.sh
/worktrees/
/.ruff_cache/
6 changes: 5 additions & 1 deletion Makefile
Expand Up @@ -14,10 +14,14 @@ format:

.PHONY: lint
lint:
flake8 $(sources)
ruff $(sources)
isort $(sources) --check-only --df
black $(sources) --check --diff

.PHONY: lint-flake8
lint-flake8:
flake8 $(sources)

.PHONY: mypy
mypy:
mypy pydantic docs/build
Expand Down
2 changes: 1 addition & 1 deletion pydantic/color.py
Expand Up @@ -173,7 +173,7 @@ def as_hsl_tuple(self, *, alpha: Optional[bool] = None) -> HslColorTuple:
True - always include alpha,
False - always omit alpha,
"""
h, l, s = rgb_to_hls(self._rgba.r, self._rgba.g, self._rgba.b)
h, l, s = rgb_to_hls(self._rgba.r, self._rgba.g, self._rgba.b) # noqa: E741
if alpha is None:
if self._rgba.alpha is None:
return h, s, l
Expand Down
2 changes: 1 addition & 1 deletion pydantic/main.py
Expand Up @@ -115,7 +115,7 @@ class BaseModel(_repr.Representation, metaclass=ModelMetaclass):
__pydantic_validator_functions__: typing.ClassVar[_validation_functions.ValidationFunctions]
__fields__: typing.ClassVar[dict[str, FieldInfo]] = {}
__config__: typing.ClassVar[type[BaseConfig]] = BaseConfig
__json_encoder__: typing.ClassVar[typing.Callable[[Any], Any]] = lambda x: x
__json_encoder__: typing.ClassVar[typing.Callable[[Any], Any]] = lambda x: x # noqa: E731
__schema_cache__: typing.ClassVar[dict[Any, Any]] = {}
__signature__: typing.ClassVar[Signature]
__private_attributes__: typing.ClassVar[dict[str, ModelPrivateAttr]]
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Expand Up @@ -109,6 +109,12 @@ per_file_ignores = [

[tool.ruff]
line-length = 120
extend-select = ['Q']
flake8-quotes = {inline-quotes = 'single', multiline-quotes = 'double'}

[tool.ruff.per-file-ignores]
'pydantic/__init__.py' = ['F405', 'F403']
'tests/test_forward_ref.py' = ['F821']

[tool.coverage.run]
source = ['pydantic']
Expand Down
1 change: 1 addition & 0 deletions requirements/linting.in
Expand Up @@ -10,3 +10,4 @@ pydantic-core
pyupgrade
mypy
pre-commit
ruff
2 changes: 2 additions & 0 deletions requirements/linting.txt
Expand Up @@ -63,6 +63,8 @@ pyupgrade==2.37.3
# via -r requirements/linting.in
pyyaml==6.0
# via pre-commit
ruff==0.0.95
# via -r requirements/linting.in
sortedcontainers==2.4.0
# via hypothesis
tokenize-rt==4.2.1
Expand Down
2 changes: 1 addition & 1 deletion tests/mypy/modules/plugin_fail.py
Expand Up @@ -237,7 +237,7 @@ class FieldDefaultTestingModel(BaseModel):
g: str = Field(default_factory=set)
h: int = Field(default_factory=_default_factory)
i: List[int] = Field(default_factory=list)
l: str = Field(default_factory=3)
l_: str = Field(default_factory=3)

# Default and default factory
m: int = Field(default=1, default_factory=list)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_dataclasses.py
Expand Up @@ -1209,11 +1209,11 @@ class Users(BaseModel):
def test_discriminated_union_basemodel_instance_value():
@pydantic.dataclasses.dataclass
class A:
l: Literal['a']
l: Literal['a'] # noqa: E741

@pydantic.dataclasses.dataclass
class B:
l: Literal['b']
l: Literal['b'] # noqa: E741

@pydantic.dataclasses.dataclass
class Top:
Expand Down Expand Up @@ -1360,7 +1360,7 @@ def __post_init__(self):
def test_self_reference_dataclass():
@pydantic.dataclasses.dataclass
class MyDataclass:
self_reference: 'MyDataclass'
self_reference: 'MyDataclass' # noqa: F821

assert MyDataclass.__pydantic_model__.__fields__['self_reference'].type_ is MyDataclass

Expand Down
34 changes: 17 additions & 17 deletions tests/test_discrimated_union.py
Expand Up @@ -107,7 +107,7 @@ class Dog(BaseModel):

class Lizard(BaseModel):
pet_type: Literal['reptile', 'lizard']
l: str
m: str

class Model(BaseModel):
pet: Annotated[Union[Cat, Dog, Lizard], Field(discriminator='pet_type')]
Expand Down Expand Up @@ -257,15 +257,15 @@ class Model(BaseModel):

def test_discriminated_union_basemodel_instance_value():
class A(BaseModel):
l: Literal['a']
foo: Literal['a']

class B(BaseModel):
l: Literal['b']
foo: Literal['b']

class Top(BaseModel):
sub: Union[A, B] = Field(..., discriminator='l')
sub: Union[A, B] = Field(..., discriminator='foo')

t = Top(sub=A(l='a'))
t = Top(sub=A(foo='a'))
assert isinstance(t, Top)


Expand All @@ -289,23 +289,23 @@ class Top(BaseModel):

def test_discriminated_union_int():
class A(BaseModel):
l: Literal[1]
m: Literal[1]

class B(BaseModel):
l: Literal[2]
m: Literal[2]

class Top(BaseModel):
sub: Union[A, B] = Field(..., discriminator='l')

assert isinstance(Top.parse_obj({'sub': {'l': 2}}).sub, B)
assert isinstance(Top.parse_obj({'sub': {'m': 2}}).sub, B)
with pytest.raises(ValidationError) as exc_info:
Top.parse_obj({'sub': {'l': 3}})
Top.parse_obj({'sub': {'m': 3}})
assert exc_info.value.errors() == [
{
'loc': ('sub',),
'msg': "No match for discriminator 'l' and value 3 (allowed values: 1, 2)",
'type': 'value_error.discriminated_union.invalid_discriminator',
'ctx': {'discriminator_key': 'l', 'discriminator_value': 3, 'allowed_values': '1, 2'},
'ctx': {'discriminator_key': 'm', 'discriminator_value': 3, 'allowed_values': '1, 2'},
}
]

Expand All @@ -316,24 +316,24 @@ class EnumValue(Enum):
b = 2

class A(BaseModel):
l: Literal[EnumValue.a]
m: Literal[EnumValue.a]

class B(BaseModel):
l: Literal[EnumValue.b]
m: Literal[EnumValue.b]

class Top(BaseModel):
sub: Union[A, B] = Field(..., discriminator='l')
sub: Union[A, B] = Field(..., discriminator='m')

assert isinstance(Top.parse_obj({'sub': {'l': EnumValue.b}}).sub, B)
assert isinstance(Top.parse_obj({'sub': {'m': EnumValue.b}}).sub, B)
with pytest.raises(ValidationError) as exc_info:
Top.parse_obj({'sub': {'l': 3}})
Top.parse_obj({'sub': {'m': 3}})
assert exc_info.value.errors() == [
{
'loc': ('sub',),
'msg': "No match for discriminator 'l' and value 3 (allowed values: <EnumValue.a: 1>, <EnumValue.b: 2>)",
'msg': "No match for discriminator 'm' and value 3 (allowed values: <EnumValue.a: 1>, <EnumValue.b: 2>)",
'type': 'value_error.discriminated_union.invalid_discriminator',
'ctx': {
'discriminator_key': 'l',
'discriminator_key': 'm',
'discriminator_value': 3,
'allowed_values': '<EnumValue.a: 1>, <EnumValue.b: 2>',
},
Expand Down
2 changes: 1 addition & 1 deletion tests/test_forward_ref.py
Expand Up @@ -122,7 +122,7 @@ class Foo(BaseModel):
def test_self_forward_ref_collection(create_module):
@create_module
def module():
from typing import Dict, List
from typing import Dict, List # noqa: F401

from pydantic import BaseModel

Expand Down
2 changes: 1 addition & 1 deletion tests/test_generics.py
Expand Up @@ -1061,7 +1061,7 @@ def module():
T = TypeVar('T')

class Model1(GenericModel, Generic[T]):
ref: 'Model2[T]'
ref: 'Model2[T]' # noqa: F821

class Model2(GenericModel, Generic[T]):
ref: Union[T, Model1[T]]
Expand Down
3 changes: 2 additions & 1 deletion tests/test_hypothesis_plugin.py
Expand Up @@ -11,7 +11,8 @@
except ImportError:
from unittest import mock

given = settings = lambda *a, **kw: (lambda f: f) # pass-through decorator
# pass-through decorator
given = settings = lambda *a, **kw: (lambda f: f) # noqa: E731
HealthCheck = st = mock.Mock()

pytestmark = pytest.mark.skipif(True, reason='"hypothesis" not installed')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_types_payment_card_number.py
Expand Up @@ -19,7 +19,7 @@

# Mock PaymentCardNumber
PCN = namedtuple('PaymentCardNumber', ['card_number', 'brand'])
PCN.__len__ = lambda v: len(v.card_number)
PCN.__len__ = lambda v: len(v.card_number) # noqa: E731


@pytest.fixture(scope='session', name='PaymentCard')
Expand Down

0 comments on commit a57346a

Please sign in to comment.