Skip to content

fix __new__ typing for OpenApiModel #2433

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions .generator/src/generator/templates/model_utils.j2
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import pprint
import re
import tempfile
from types import MappingProxyType
from typing import Collection, Mapping, Union
from typing_extensions import Final
from typing import Collection, Mapping, Union, overload
from typing_extensions import Final, Self

from dateutil.parser import parse

Expand Down Expand Up @@ -89,7 +89,7 @@ def composed_model_input_classes(cls):
return []


class OpenApiModel(object):
class OpenApiModel:
"""The base class for all OpenAPIModels.

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

@overload
def __new__(cls, arg: None) -> None: # type: ignore
Copy link
Contributor Author

@ava-silver ava-silver Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to add the type ignore because mypy (in CI) doesn't like this, but we do this in the code already, we just didn't reflect that in the typing before

...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a difference between this as pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope! ... (or the singleton Ellipsis) in python is just a convention used to indicate that it is not the real implementation, whereas pass is usually used when the implementation is intentionally blank (like with try: ... except: pass)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tldr they both do nothing, but conventionally you would use ellipses for overload signature bodies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs for overload also use ... in their example usage.


@overload
def __new__(cls, arg: "ModelComposed") -> Self:
...

@overload
def __new__(cls, *args, **kwargs) -> Self:
...

def __new__(cls, *args, **kwargs):
if len(args) == 1:
arg = args[0]
Expand All @@ -185,7 +197,7 @@ class OpenApiModel(object):
oneof_instance = get_oneof_instance(cls, model_kwargs, kwargs, model_arg=arg)
return oneof_instance

return super(OpenApiModel, cls).__new__(cls)
return super().__new__(cls)

def __init__(self, kwargs):
"""
Expand Down
20 changes: 16 additions & 4 deletions src/datadog_api_client/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import re
import tempfile
from types import MappingProxyType
from typing import Collection, Mapping, Union
from typing_extensions import Final
from typing import Collection, Mapping, Union, overload
from typing_extensions import Final, Self

from dateutil.parser import parse

Expand Down Expand Up @@ -91,7 +91,7 @@ def composed_model_input_classes(cls):
return []


class OpenApiModel(object):
class OpenApiModel:
"""The base class for all OpenAPIModels.

:var attribute_map: The key is attribute name and the value is json
Expand Down Expand Up @@ -186,6 +186,18 @@ def __getattr__(self, attr):
"""Get the value of an attribute using dot notation: `instance.attr`."""
return self.__getitem__(attr)

@overload
def __new__(cls, arg: None) -> None: # type: ignore
...

@overload
def __new__(cls, arg: "ModelComposed") -> Self:
...

@overload
def __new__(cls, *args, **kwargs) -> Self:
...

def __new__(cls, *args, **kwargs):
if len(args) == 1:
arg = args[0]
Expand All @@ -198,7 +210,7 @@ def __new__(cls, *args, **kwargs):
oneof_instance = get_oneof_instance(cls, model_kwargs, kwargs, model_arg=arg)
return oneof_instance

return super(OpenApiModel, cls).__new__(cls)
return super().__new__(cls)

def __init__(self, kwargs):
"""
Expand Down