Skip to content

Commit 35628c1

Browse files
authored
[TYPING] fix __new__ typing for OpenApiModel (#2433)
1 parent 7bf40c9 commit 35628c1

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

.generator/src/generator/templates/model_utils.j2

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import pprint
1111
import re
1212
import tempfile
1313
from types import MappingProxyType
14-
from typing import Collection, Mapping, Union
15-
from typing_extensions import Final
14+
from typing import Collection, Mapping, Union, overload
15+
from typing_extensions import Final, Self
1616

1717
from dateutil.parser import parse
1818

@@ -89,7 +89,7 @@ def composed_model_input_classes(cls):
8989
return []
9090

9191

92-
class OpenApiModel(object):
92+
class OpenApiModel:
9393
"""The base class for all OpenAPIModels.
9494

9595
:var attribute_map: The key is attribute name and the value is json
@@ -173,6 +173,18 @@ class OpenApiModel(object):
173173
"""Get the value of an attribute using dot notation: `instance.attr`."""
174174
return self.__getitem__(attr)
175175

176+
@overload
177+
def __new__(cls, arg: None) -> None: # type: ignore
178+
...
179+
180+
@overload
181+
def __new__(cls, arg: "ModelComposed") -> Self:
182+
...
183+
184+
@overload
185+
def __new__(cls, *args, **kwargs) -> Self:
186+
...
187+
176188
def __new__(cls, *args, **kwargs):
177189
if len(args) == 1:
178190
arg = args[0]
@@ -185,7 +197,7 @@ class OpenApiModel(object):
185197
oneof_instance = get_oneof_instance(cls, model_kwargs, kwargs, model_arg=arg)
186198
return oneof_instance
187199

188-
return super(OpenApiModel, cls).__new__(cls)
200+
return super().__new__(cls)
189201

190202
def __init__(self, kwargs):
191203
"""

src/datadog_api_client/model_utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import re
1414
import tempfile
1515
from types import MappingProxyType
16-
from typing import Collection, Mapping, Union
17-
from typing_extensions import Final
16+
from typing import Collection, Mapping, Union, overload
17+
from typing_extensions import Final, Self
1818

1919
from dateutil.parser import parse
2020

@@ -91,7 +91,7 @@ def composed_model_input_classes(cls):
9191
return []
9292

9393

94-
class OpenApiModel(object):
94+
class OpenApiModel:
9595
"""The base class for all OpenAPIModels.
9696
9797
:var attribute_map: The key is attribute name and the value is json
@@ -186,6 +186,18 @@ def __getattr__(self, attr):
186186
"""Get the value of an attribute using dot notation: `instance.attr`."""
187187
return self.__getitem__(attr)
188188

189+
@overload
190+
def __new__(cls, arg: None) -> None: # type: ignore
191+
...
192+
193+
@overload
194+
def __new__(cls, arg: "ModelComposed") -> Self:
195+
...
196+
197+
@overload
198+
def __new__(cls, *args, **kwargs) -> Self:
199+
...
200+
189201
def __new__(cls, *args, **kwargs):
190202
if len(args) == 1:
191203
arg = args[0]
@@ -198,7 +210,7 @@ def __new__(cls, *args, **kwargs):
198210
oneof_instance = get_oneof_instance(cls, model_kwargs, kwargs, model_arg=arg)
199211
return oneof_instance
200212

201-
return super(OpenApiModel, cls).__new__(cls)
213+
return super().__new__(cls)
202214

203215
def __init__(self, kwargs):
204216
"""

0 commit comments

Comments
 (0)