Skip to content

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

Closed
@packyg

Description

@packyg

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions