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

[BUG] Type checker does not recognize fields on ModelSchema Meta #1382

Open
Tragio opened this issue Jan 4, 2025 · 3 comments
Open

[BUG] Type checker does not recognize fields on ModelSchema Meta #1382

Tragio opened this issue Jan 4, 2025 · 3 comments

Comments

@Tragio
Copy link

Tragio commented Jan 4, 2025

Describe the bug
Hi there! 😃 When using a ModelSchema with a Meta class that defines the model and fields, the type checker does not recognize the fields defined in the Meta class.

Code Example

class PhoneNumberSchema(ModelSchema):
    class Meta:
        model = PhoneNumber
        fields = ["dial_code", "phone_number"]

    @http_post(
        "/test",
        auth=None,
        response={status.HTTP_201_CREATED: None},
    )
    def test(self, request, payload: PhoneNumberSchema):
        print(payload.dial_code)

Pylance outputs:

Cannot access attribute "dial_code" for class "PhoneNumberSchema"
  Attribute "dial_code" is unknownPylance[reportAttributeAccessIssue](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportAttributeAccessIssue)

However, if I do the following, I have no problems, but then I'm repeating code.

class PhoneNumberSchema(ModelSchema):
    dial_code: Optional[str] = Field(None, max_length=10)
    class Meta:
        model = PhoneNumber
        fields = ["dial_code", "phone_number"]

Not sure if there is a better way, or if I'm doing something wrong 🤔

Thank you for the incredible work and have an amazing new year 🚀

@vitalik
Copy link
Owner

vitalik commented Jan 4, 2025

there are few concerns in your code

  • def test(self, you are using class methods instead of python function and it's not clear what 'http_post` is in your case
  • missing PhoneNumber model - maybe dial_code is a special field or somehitng

so no way to debug or find any reasons

@Tragio
Copy link
Author

Tragio commented Jan 4, 2025

Hi @vitalik 👋

Sorry, you're absolutely right. 🙏 Here is the complete context that I forgot to add on my initial post.

I'm using Django Ninja Extra, where it has the @api_controller it is so seamless that I forget it is a different package. 😅 However, I also tested using only Django Ninja and the result was the same, here is the test file I used.

from ninja import ModelSchema, NinjaAPI
from ninja_extra import (
    api_controller,
    http_get,
)
from tenant.models.user import UserStubPhoneNumber

api = NinjaAPI()


class PhoneNumberSchema(ModelSchema):
    class Meta:
        model = UserStubPhoneNumber
        fields = ["dial_code", "phone_number"]


# Django Ninja Extra
@api_controller("/users", tags=["User"])
class UserAPI:
    @http_get("/test", response=PhoneNumberSchema)
    def test(self, request, payload: PhoneNumberSchema):
        print(payload.dial_code)


# Django Ninja
@api.get("/test", response=PhoneNumberSchema)
def test(request, data: PhoneNumberSchema):
    print(data.dial_code)

CleanShot 2025-01-04 at 21 52 55@2x

Thank you very much 😃

@pinguin999
Copy link
Contributor

Hi @Tragio

here is an example how I worked around the problem:

class UserBodySchema(ModelSchema):
    if TYPE_CHECKING:
        date: datetime_date
        belly: Decimal
        waist: Decimal
        hip: Decimal

    class Meta:
        model = UserBody
        fields = [
            "date",
            "belly",
            "waist",
            "hip",
        ]

It's not perfect but it's working.

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

No branches or pull requests

3 participants