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

Forbid kwarg unpacking in class definition #1754

Closed
sobolevn opened this issue Dec 1, 2020 · 10 comments · Fixed by #2217
Closed

Forbid kwarg unpacking in class definition #1754

sobolevn opened this issue Dec 1, 2020 · 10 comments · Fixed by #2217
Assignees
Labels
help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Milestone

Comments

@sobolevn
Copy link
Member

sobolevn commented Dec 1, 2020

Rule request

Thesis

We need to forbid code like this:

class Some(Base, **kwargs):
    ...

But, this code is valid:

class Some(Base, arg=1):
    ...

Reasoning

Dynamic class generation with unknown values is really bad!

@sobolevn sobolevn added help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule labels Dec 1, 2020
@sobolevn sobolevn added this to the Version 0.15 aka Python3.9 milestone Dec 1, 2020
@StutiV
Copy link

StutiV commented Dec 11, 2020

Hello, @sobolevn Can you assign me this issue? I'd like to contribute. Thanks.

@sobolevn
Copy link
Member Author

Thanks a lot!

@StutiV
Copy link

StutiV commented Dec 11, 2020

Thank you, @sobolevn ! Can you please elaborate the issue? It'd be really helpful.

@sobolevn
Copy link
Member Author

@StutiV what help do you need? I would love to answer speicific questions 🙂

@StutiV
Copy link

StutiV commented Dec 11, 2020

@sobolevn Which file should I be making these changes in?

@sobolevn
Copy link
Member Author

@StutiV it is probably ast/classes.py. Feel free to choose the best visitor for the job.
https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/visitors/ast/classes.py

Probably WrongClassVisitor

@StutiV StutiV removed their assignment Dec 25, 2020
@StakanAgan
Copy link

@sobolevn can you assign this issue for me?

@sobolevn
Copy link
Member Author

@StakanAgan done! Thanks!

@thepabloaguilar
Copy link
Contributor

@sobolevn do you have an example full example where a class receives parameters, please? Does the class need to have a metaclass or something like?

@sobolevn
Copy link
Member Author

For example, this is fine:

from pydantic import BaseModel, Extra

class Model(BaseModel, extra=Extra.forbid):
    a: str

This is not (but will work the same way):

from pydantic import BaseModel, Extra

kw = {'extra': Extra.forbid}
class Model(BaseModel, **kw):
    a: str

Source: https://pydantic-docs.helpmanual.io/usage/model_config/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Projects
None yet
4 participants