Skip to content

Change <Model>.from_dict() to a @classmethod #215

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

Closed
packyg opened this issue Oct 13, 2020 · 1 comment · Fixed by #292
Closed

Change <Model>.from_dict() to a @classmethod #215

packyg opened this issue Oct 13, 2020 · 1 comment · Fixed by #292
Labels
✨ enhancement New feature or improvement 👋 good first issue Good for newcomers

Comments

@packyg
Copy link
Contributor

packyg commented Oct 13, 2020

Is your feature request related to a problem? Please describe.
I want to be able to extend a model generated by this project in my business logic. A basic example would be:

# GeneratedModel generated by openapi-python-client

class EnhancedGeneratedModel(GeneratedModel):
  def id_eq(self, other: GeneratedModel) -> bool:
     return self.id == other.id

I cannot use EnhancedGeneratedModel.from_dict as that would return a GeneratedModel.

Describe the solution you'd like
Update the from_dict template in openapi_python_client/templates/model.pyi to be a @classmethod instead of a @staticmethod and use the passed class object to instantiate the model.

i.e. change from

@staticmethod
def from_dict(d: Dict[str, Any]) -> "{{ model.reference.class_name }}":
  # ... 
  return {{ model.reference.class_name }}(...)

to

@classmethod
def from_dict(cls, d: Dict[str, Any]) -> "{{ model.reference.class_name }}":
  # ... 
  return cls(...)

Describe alternatives you've considered
Manually creating a from_dict method on the extended class.

@packyg packyg added the ✨ enhancement New feature or improvement label Oct 13, 2020
@dbanty
Copy link
Collaborator

dbanty commented Oct 13, 2020

Sure, I don't see a problem with that. I do think we need to switch the return type to a TypeVar to have the types be more accurate.

I'm a bit slow on OSS dev for the next couple months, so feel free to send a PR if you want it faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or improvement 👋 good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants