Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLW3201 false positive for Pydantic dunders like __get_pydantic_core_schema__ #15624

Closed
jamesbraza opened this issue Jan 20, 2025 · 1 comment
Labels
rule Implementing or modifying a lint rule

Comments

@jamesbraza
Copy link
Contributor

With the following dependencies and Python 3.12.7:

pydantic                  2.10.1
pydantic-core             2.27.1
pylint                    3.3.3
pylint-plugin-utils       0.8.2
pylint-pydantic           0.3.5
ruff                      0.9.2

Running Ruff on the snippet from https://docs.pydantic.dev/latest/concepts/types/#as-a-method-on-a-custom-type

from collections.abc import Callable
from dataclasses import dataclass
from typing import Any

from pydantic import GetCoreSchemaHandler
from pydantic_core import CoreSchema, core_schema


@dataclass(frozen=True)
class MyAfterValidator:
    func: Callable[[Any], Any]

    def __get_pydantic_core_schema__(
        self, source_type: Any, handler: GetCoreSchemaHandler
    ) -> CoreSchema:
        return core_schema.no_info_after_validator_function(
            self.func, handler(source_type)
        )

I get the following PLW3201 error:

a.py: PLW3201 Dunder method `__get_pydantic_core_schema__` has no special meaning in Python 3

I know I can allowlist this name via Ruff configuration:

[tool.ruff.lint.pylint]
allow-dunder-method-names = ["__get_pydantic_core_schema__"]

However, I guess the question is similar to #9716, but instead of built-ins now for Pydantic. Should Pydantic dunders be ignored by default?

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Jan 21, 2025
@MichaReiser
Copy link
Member

Thanks for the detailed write up and linking to the other relevant issue.

Adding external libraries is always a slippery slope because once you add one, you have to add all others. But we have made exceptions for libraries that can be considered community standards as which pydantic qualifies.

But I think it ultimately comes back to what Alex said in his comment and if allowing any non-stdlib dunder is in the indent of the rule, because the rule very explicitly states that it checks for dunder methods that have no special meaning in Python 3, which is true for all pydantic dunders (it's the framework that adds the special meaning).

Either way, I'll merge this with #9716 (comment) as it's basically the same question but for pydandic instead of rich.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

2 participants