Skip to content

Fix _BaseConfigurator.__new__ crash for subclasses with __init__ arguments#5441

Open
Eason09053360 wants to merge 3 commits into
open-telemetry:mainfrom
Eason09053360:fix-baseconfigurator-new-with-args
Open

Fix _BaseConfigurator.__new__ crash for subclasses with __init__ arguments#5441
Eason09053360 wants to merge 3 commits into
open-telemetry:mainfrom
Eason09053360:fix-baseconfigurator-new-with-args

Conversation

@Eason09053360

@Eason09053360 Eason09053360 commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Description

_BaseConfigurator.__new__ forwards *args, **kwargs to object.__new__. Since _BaseConfigurator overrides __new__, CPython's object.__new__ raises TypeError: object.__new__() takes exactly one argument when it receives any extra arguments. As a result, any distro Configurator subclass that defines an __init__ with parameters (an extension pattern the docs encourage) crashes on first instantiation.

Fix: call object.__new__(cls) without forwarding arguments — type.__call__ still passes them to __init__. The singleton behavior is unchanged.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added test_custom_configurator_with_init_args, which fails with the TypeError above before the fix and passes after.
  • Existing TestConfigurator tests still pass.

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Eason09053360 added a commit to Eason09053360/opentelemetry-python that referenced this pull request Jul 19, 2026
@Eason09053360
Eason09053360 marked this pull request as ready for review July 20, 2026 11:33
@Eason09053360
Eason09053360 requested a review from a team as a code owner July 20, 2026 11:33
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls, *args, **kwargs)
cls._instance = object.__new__(cls)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
cls._instance = object.__new__(cls)
cls._instance = super().__new__(cls)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion!!
Applied

Eason09053360 and others added 3 commits July 24, 2026 09:00
…ments

object.__new__ raises TypeError when passed extra arguments if __new__
is overridden, so any Configurator subclass defining an __init__ with
parameters crashed on first instantiation. Call object.__new__(cls)
without forwarding arguments; type.__call__ still passes them to
__init__.
Follows cooperative multiple inheritance conventions so the MRO's
__new__ chain isn't silently skipped if a mixin is added later.

Co-authored-by: herin049 <40302054+herin049@users.noreply.github.com>
@Eason09053360
Eason09053360 force-pushed the fix-baseconfigurator-new-with-args branch from 8611a1c to 0f6abda Compare July 24, 2026 01:00
@xrmx xrmx moved this to Easy to review / merge / close in Python PR digest Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Easy to review / merge / close

Development

Successfully merging this pull request may close these issues.

3 participants