Skip to content

RUF012 warns for pydantic.v1.main.BaseModel #21953

@justin-snyder-slgg

Description

@justin-snyder-slgg

Summary

The folks at Pydantic somewhat recently added an alternate migration path which allows you to use v1 from v2.

Ruff does not correctly identify this case as a valid exception to RUF012

Example:

from pydantic.v1.main import BaseModel

class Foo(BaseModel):
    bar: list[str] = ['baz']  # <- triggers RUF012

Fix:

from pydantic.v1 import BaseModel  # removing the .main here works

class Foo(BaseModel):
    bar: list[str] = ['baz']  # <- triggers RUF012

Perhaps it would be a good idea to add a setting to mark exceptions to this rule...

Or maybe there's another rule I should activate to force the simpler import since pyright is still really bad at prioritizing the right import paths (often suggesting indirect imports through third party libraries or subpackages even when stuff is exposed from the top level module of a library)

Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions