mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-03 00:43:46 +00:00
python: type generated client using Self (#16693)
* python: type generated client using Self This doesn't offer a clear win but this helps for: * Using modern types and making the typing intent clearer * Decreasing the need for `from __future__ import annotations`, since a class can now refer to itself without using its name * Using more `cls` to automatically refer to the class, instead of respecifying the class name every time Self is available from Python 3.11 and is provided in typing_extensions (since 4.0.0) as a fallback for older versions See: https://peps.python.org/pep-0673/ See: https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#added-in-version-400 * generate code
This commit is contained in:
@@ -21,6 +21,11 @@ import json
|
||||
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, StrictStr
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class Bird(BaseModel):
|
||||
"""
|
||||
@@ -46,7 +51,7 @@ class Bird(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> Bird:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of Bird from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -59,15 +64,15 @@ class Bird(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> Bird:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of Bird from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return Bird.model_validate(obj)
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = Bird.model_validate({
|
||||
_obj = cls.model_validate({
|
||||
"size": obj.get("size"),
|
||||
"color": obj.get("color")
|
||||
})
|
||||
|
||||
@@ -21,6 +21,11 @@ import json
|
||||
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, StrictInt, StrictStr
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class Category(BaseModel):
|
||||
"""
|
||||
@@ -46,7 +51,7 @@ class Category(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> Category:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of Category from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -59,15 +64,15 @@ class Category(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> Category:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of Category from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return Category.model_validate(obj)
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = Category.model_validate({
|
||||
_obj = cls.model_validate({
|
||||
"id": obj.get("id"),
|
||||
"name": obj.get("name")
|
||||
})
|
||||
|
||||
@@ -23,6 +23,11 @@ from typing import Optional
|
||||
from pydantic import StrictStr
|
||||
from pydantic import Field
|
||||
from openapi_client.models.query import Query
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class DataQuery(Query):
|
||||
"""
|
||||
@@ -49,7 +54,7 @@ class DataQuery(Query):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> DataQuery:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of DataQuery from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -62,15 +67,15 @@ class DataQuery(Query):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> DataQuery:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of DataQuery from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return DataQuery.model_validate(obj)
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = DataQuery.model_validate({
|
||||
_obj = cls.model_validate({
|
||||
"id": obj.get("id"),
|
||||
"outcomes": obj.get("outcomes"),
|
||||
"suffix": obj.get("suffix"),
|
||||
|
||||
@@ -22,6 +22,11 @@ import json
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel, StrictInt, StrictStr, field_validator
|
||||
from openapi_client.models.string_enum_ref import StringEnumRef
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class DefaultValue(BaseModel):
|
||||
"""
|
||||
@@ -64,7 +69,7 @@ class DefaultValue(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> DefaultValue:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of DefaultValue from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -92,15 +97,15 @@ class DefaultValue(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> DefaultValue:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of DefaultValue from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return DefaultValue.model_validate(obj)
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = DefaultValue.model_validate({
|
||||
_obj = cls.model_validate({
|
||||
"array_string_enum_ref_default": obj.get("array_string_enum_ref_default"),
|
||||
"array_string_enum_default": obj.get("array_string_enum_default"),
|
||||
"array_string_default": obj.get("array_string_default"),
|
||||
|
||||
@@ -23,6 +23,11 @@ from typing import Optional, Union
|
||||
from pydantic import BaseModel, StrictFloat, StrictInt
|
||||
from pydantic import Field
|
||||
from typing_extensions import Annotated
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class NumberPropertiesOnly(BaseModel):
|
||||
"""
|
||||
@@ -49,7 +54,7 @@ class NumberPropertiesOnly(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> NumberPropertiesOnly:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of NumberPropertiesOnly from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -62,15 +67,15 @@ class NumberPropertiesOnly(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> NumberPropertiesOnly:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of NumberPropertiesOnly from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return NumberPropertiesOnly.model_validate(obj)
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = NumberPropertiesOnly.model_validate({
|
||||
_obj = cls.model_validate({
|
||||
"number": obj.get("number"),
|
||||
"float": obj.get("float"),
|
||||
"double": obj.get("double")
|
||||
|
||||
@@ -24,6 +24,11 @@ from pydantic import BaseModel, StrictInt, StrictStr, field_validator
|
||||
from pydantic import Field
|
||||
from openapi_client.models.category import Category
|
||||
from openapi_client.models.tag import Tag
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class Pet(BaseModel):
|
||||
"""
|
||||
@@ -63,7 +68,7 @@ class Pet(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> Pet:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of Pet from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -86,15 +91,15 @@ class Pet(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> Pet:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of Pet from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return Pet.model_validate(obj)
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = Pet.model_validate({
|
||||
_obj = cls.model_validate({
|
||||
"id": obj.get("id"),
|
||||
"name": obj.get("name"),
|
||||
"category": Category.from_dict(obj.get("category")) if obj.get("category") is not None else None,
|
||||
|
||||
@@ -22,6 +22,11 @@ import json
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel, StrictInt, StrictStr, field_validator
|
||||
from pydantic import Field
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class Query(BaseModel):
|
||||
"""
|
||||
@@ -58,7 +63,7 @@ class Query(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> Query:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of Query from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -71,7 +76,7 @@ class Query(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> Query:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of Query from a dict"""
|
||||
|
||||
|
||||
|
||||
@@ -21,6 +21,10 @@ from enum import Enum
|
||||
|
||||
|
||||
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
|
||||
class StringEnumRef(str, Enum):
|
||||
@@ -36,8 +40,8 @@ class StringEnumRef(str, Enum):
|
||||
UNCLASSIFIED = 'unclassified'
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> StringEnumRef:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of StringEnumRef from a JSON string"""
|
||||
return StringEnumRef(json.loads(json_str))
|
||||
return cls(json.loads(json_str))
|
||||
|
||||
|
||||
|
||||
@@ -21,6 +21,11 @@ import json
|
||||
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, StrictInt, StrictStr
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class Tag(BaseModel):
|
||||
"""
|
||||
@@ -46,7 +51,7 @@ class Tag(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> Tag:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of Tag from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -59,15 +64,15 @@ class Tag(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> Tag:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of Tag from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return Tag.model_validate(obj)
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = Tag.model_validate({
|
||||
_obj = cls.model_validate({
|
||||
"id": obj.get("id"),
|
||||
"name": obj.get("name")
|
||||
})
|
||||
|
||||
@@ -21,6 +21,11 @@ import json
|
||||
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, StrictInt, StrictStr
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter(BaseModel):
|
||||
"""
|
||||
@@ -48,7 +53,7 @@ class TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter(BaseMod
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -61,15 +66,15 @@ class TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter(BaseMod
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.model_validate(obj)
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = TestQueryStyleDeepObjectExplodeTrueObjectAllOfQueryObjectParameter.model_validate({
|
||||
_obj = cls.model_validate({
|
||||
"size": obj.get("size"),
|
||||
"color": obj.get("color"),
|
||||
"id": obj.get("id"),
|
||||
|
||||
@@ -21,6 +21,11 @@ import json
|
||||
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel, StrictStr
|
||||
from typing import Dict, Any
|
||||
try:
|
||||
from typing import Self
|
||||
except ImportError:
|
||||
from typing_extensions import Self
|
||||
|
||||
class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter(BaseModel):
|
||||
"""
|
||||
@@ -45,7 +50,7 @@ class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter(BaseModel):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter:
|
||||
def from_json(cls, json_str: str) -> Self:
|
||||
"""Create an instance of TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter from a JSON string"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
@@ -58,15 +63,15 @@ class TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter(BaseModel):
|
||||
return _dict
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter:
|
||||
def from_dict(cls, obj: dict) -> Self:
|
||||
"""Create an instance of TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter.model_validate(obj)
|
||||
return cls.model_validate(obj)
|
||||
|
||||
_obj = TestQueryStyleFormExplodeTrueArrayStringQueryObjectParameter.model_validate({
|
||||
_obj = cls.model_validate({
|
||||
"values": obj.get("values")
|
||||
})
|
||||
return _obj
|
||||
|
||||
Reference in New Issue
Block a user