Skip to content

Generic.__new__ should pass args/kwargs to super-class __new__ #514

Description

@chadrik

Is there a reason that Generic.__new__ does not pass *args and **kwargs to the super class's __new__? These are required in some cases for proper behavior.

Here's a somewhat non-sensical example that demonstrates the problem:

from __future__ import absolute_import, print_function

from typing import Generic, TypeVar
T = TypeVar('T')

class BaseThing(Generic[T]):
    def __new__(cls, *args, **kwargs):
        kwargs.setdefault('encoding', 'utf-8')
        return super(BaseThing, cls).__new__(cls, *args, **kwargs)

class Str(BaseThing, unicode):
    pass

s = Str('blarg')
print(repr(s))

s is u'' instead of u'blarg'.

The offending code is in the typing._generic_new() helper.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions