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

Subclass's Meta should inherit from parent class's Meta #1118

Open
sashkent3 opened this issue Feb 15, 2025 · 8 comments
Open

Subclass's Meta should inherit from parent class's Meta #1118

sashkent3 opened this issue Feb 15, 2025 · 8 comments

Comments

@sashkent3
Copy link

Every time the generated code contains a structure similar to:

class A:
  class Meta:
    pass

class B(A):
  class Meta:
    pass

I get a static type-checker warning about B.Meta being an incorrect override for A.Meta. To fix this, simply change the above to:

class A:
  class Meta:
    pass

class B(A):
  class Meta(A.Meta):
    pass
@tefra
Copy link
Owner

tefra commented Mar 1, 2025

Can you give some more info about the type-checker you are using?

Unfortunately this isn't really an option as the Meta class must be final to avoid conflicts

@sashkent3
Copy link
Author

@tefra It's Pyright, take a look here.

@tefra
Copy link
Owner

tefra commented Mar 1, 2025

Thanks @sashkent3 from quick look a lot of libraries suffer from that issue

microsoft/pyright#6570
typeddjango/django-stubs#748

I am open to suggestions but inheritance between Meta classes is impossible

@sashkent3
Copy link
Author

@tefra could you kindly clarify what do you mean by:

Meta class must be final

Also, if xsdata intends to ignore this warning, maybe it's better to simply generate type: ignore alongside?

@tefra
Copy link
Owner

tefra commented Mar 1, 2025

The parent class might not have a meta class, but the parent parent might, resolving the imports is very hard in these cases.

Maybe the type: ignore is the way to go let me think about it.

@sashkent3
Copy link
Author

Isn't it feasible to generate class Meta for every class? Even in cases where it's currently not needed.

@tefra
Copy link
Owner

tefra commented Mar 2, 2025

If we create a Meta class for every model and include all the properties in each one, doesn’t that undermine the point of using inheritance, which is to share and build on common attributes efficiently?

I am not sure I like adding more code/noise just to satisfy pyright

@sashkent3
Copy link
Author

Well, that's fair, but I believe this is about more than satisfying Pyright. When you do:

class A:
  class Meta:
    pass

class B(A):
  pass

The B class inherits A.Meta whether you intend it or not. The following two pieces of code are exactly equivalent, because B.Meta is A.Meta:

class C(B):
    class Meta(B.Meta):
        pass

and

class C(B):
    class Meta(A.Meta):
        pass

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

2 participants