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

Signature of dataclass does not support field name alias which is defined with std-dataclass field metadata #8367

Closed
1 task done
shinaji opened this issue Dec 14, 2023 · 4 comments
Labels
bug V2 Bug related to Pydantic V2 good first issue

Comments

@shinaji
Copy link

shinaji commented Dec 14, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Hello, I found that the signature of dataclass does not support field name alias which is defined with std-dataclass field metadata, even though the "ge" and so on validations are supported. Is this expected behavior at pydantic v2?

Example Code

from dataclasses import field
from pydantic.dataclasses import dataclass, Field

@dataclass
class A:
    a: int = field(metadata={"ge": 1, "alias": "aa"}) 

# In [3]: A.__signature__
# Out[3]: <Signature (a: int) -> None>  # This should be "<Signature (aa: int) -> None>"

# The "ge" validation is supported
# In [4]: A(aa=0)
# ValidationError: 1 validation error for A
# aa
#  Input should be greater than or equal to 1 [type=greater_than_equal, input_value=0, input_type=int]
#    For further information visit https://errors.pydantic.dev/2.5/v/greater_than_equal

Python, Pydantic & OS Version

pydantic version: 2.5.2
        pydantic-core version: 2.14.5
          pydantic-core build: profile=release pgo=false
               python version: 3.9.13 (main, Sep 17 2022, 22:53:18)  [Clang 14.0.0 (clang-1400.0.29.102)]
                     platform: macOS-13.6-arm64-arm-64bit
             related packages: email-validator-2.0.0.post2 fastapi-0.105.0 pydantic-extra-types-2.1.0 mypy-1.3.0 typing_extensions-4.8.0 pydantic-settings-2.0.3
@shinaji shinaji added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Dec 14, 2023
@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Dec 15, 2023
@sydney-runkle
Copy link
Member

@shinaji,

Thanks for reporting. Looks like a bug! Any interest in taking a crack at a fix?

@NeevCohen
Copy link
Contributor

NeevCohen commented Dec 16, 2023

Hey @sydney-runkle, I can try to give this a shot.

NeevCohen added a commit to NeevCohen/pydantic that referenced this issue Dec 16, 2023
Add support for field alias in dataclass signature
@NeevCohen
Copy link
Contributor

@sydney-runkle I opened a PR

@sydney-runkle
Copy link
Member

@sydney-runkle I opened a PR

Fantastic! I'll review later today :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 good first issue
Projects
None yet
Development

No branches or pull requests

3 participants