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

incompatible type error on CoreSchema by mypy #542

Closed
ytoku opened this issue Apr 11, 2023 · 6 comments · Fixed by #543
Closed

incompatible type error on CoreSchema by mypy #542

ytoku opened this issue Apr 11, 2023 · 6 comments · Fixed by #543

Comments

@ytoku
Copy link
Contributor

ytoku commented Apr 11, 2023

PR #528 added a workaround for mypy's performance problem.
But the workaround code causes the incompatible-type error when we assign a concrete schema to a CoreSchema variable or argument.

schema: core_schema.CoreSchema = core_schema.str_schema()

This is particularly problematic in defining a custom type.

code

from typing import Any

from pydantic_core import core_schema


class MyValidationType:
    @classmethod
    def __get_pydantic_core_schema__(
        cls,
        **_kwargs: Any,
    ) -> core_schema.AfterValidatorFunctionSchema:
        return core_schema.field_after_validator_function(
            function=lambda _s, _info: MyValidationType(),  # some validation
            schema=core_schema.str_schema(),  # emits incompatible-type error
        )

result

% pipenv install mypy "pydantic>=2.0a1"
% pipenv run mypy test.py
test.py:14: error: Argument "schema" to "field_after_validator_function" has incompatible type "StringSchema"; expected "Dict[str, Any]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

environment

% pipenv run pip list
Package           Version
----------------- -------
annotated-types   0.4.0
mypy              1.2.0
mypy-extensions   1.0.0
pip               23.0.1
pydantic          2.0a1
pydantic_core     0.23.1
setuptools        67.5.1
tomli             2.0.1
typing_extensions 4.5.0
wheel             0.38.4

% pipenv run python --version
Python 3.10.6
@ytoku
Copy link
Contributor Author

ytoku commented Apr 11, 2023

I suggest to use Mapping[str, Any] instead of Dict[str, Any].

This possibly causes another problem if a user try to modify the schema,
but in most case, modifying a union-typed variable is done after narrowing (or cast).

@samuelcolvin
Copy link
Member

if Mapping[str, Any] mollifies mypy, I'm good with it.

@dmontagu @adriangb?

@adriangb
Copy link
Member

I'm okay with that.

@ytoku our general stance is going to be that if you want to use pydantic-core you are probably going to need to use pyright as your type checker.

@dmontagu
Copy link
Collaborator

Mapping[str, Any] seems like the right thing anyway as we generally treat core schemas as immutable

@samuelcolvin
Copy link
Member

@ytoku would you like to create a PR?

@ytoku
Copy link
Contributor Author

ytoku commented Apr 11, 2023

I tried pyright but I found some compatibility problems. It is difficult to migrate to pyright quickly...

I'll create a PR. Thanks.

ytoku added a commit to ytoku/pydantic-core that referenced this issue Apr 11, 2023
samuelcolvin pushed a commit that referenced this issue Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants