GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Specialized sub-classes of Generic never call __init__ · Issue #192 · python/typing

Specialized sub-classes of Generic never call __init__ #192

New issue
Copy link
New issue
Copy link
Closed
Closed
Specialized sub-classes of Generic never call __init__#192
Copy link
Milestone
3.5.2

Description

@gvanrossum
gvanrossum
opened on Mar 23, 2016
Issue body actions

See http://bugs.python.org/issue26391 .

"""
A specialized sub-class of a generic type never calls __init__ when it is instantiated. See below for an example:

from typing import Generic, TypeVar

T = TypeVar('T')
class Foo(Generic[T]):
    def __init__(self, value: T):
        self.value = value

Bar = Foo[str]

foo = Foo('foo')
bar = Bar('bar')

print(type(foo), end=' ')
print(foo.value)

print(type(bar), end=' ')
print(bar.value) # AttributeError

I would expect Foo[str], Foo[int], etc to be equivalent to Foo at run-time. If this is not the case it might deserve an explicit mention in the docs. At the moment, behaviour is confusing because an instance of Foo is returned that does not have any of its attributes set.
"""

Activity

  1. gvanrossum
    added this to the 3.5.2 milestone on Mar 23, 2016
  2. gvanrossum
    added
    bug
    enhancement
    on Mar 23, 2016
  3. gvanrossum commented on Apr 1, 2016

    @gvanrossum
    gvanrossum
    on Apr 1, 2016
    MemberAuthor
    More actions

    (Update, copied from the CPython bug:)

    Actually, I just realized why this is. Generic.__new__() calls next_in_mro.__new__(_gorg(cls)). Here next_in_mro is just object, but _gorg(cls) is Foo, in the case where cls is Bar (i.e. Foo[str]). The _gorg() call strips the [str] part of the type.

    So far so good. Where it gets interesting is that, in general in Python, whenever __new__(cls, ...) returns an object whose class is not cls, it is presumed to be an already properly initialized object!

    I think I can fix this by calling __init__() explicitly in the case where _gorg(cls) is not cls.

    The reason, BTW, why it strips the [str] part of the class is because that's what PEP 484 says, in the same section where it says that NodeT and Nodeint are allowed:

    At runtime the type is not preserved, and the class of x is just
    Node in all cases.

    So this bit of code is trying to do the right thing but obviously hasn't been tested much, because mypy disallows that syntax.

  4. gvanrossum
    closed this as completedin 238502eon Apr 2, 2016
  5. gvanrossum
    added a commit that references this issue on Apr 2, 2016

    Backport fix for #192 to Python 2. Remove optimistic TODO about #196.

    37a3dd5
  6. gvanrossum commented on Apr 2, 2016

    @gvanrossum
    gvanrossum
    on Apr 2, 2016
    MemberAuthor
    More actions

    See also 37a3dd5 which backports the fix to Python 2.

  7. gvanrossum
    added 3 commits that reference this issue on Apr 2, 2016

    Backport fix for #192 to Python 2. Remove optimistic TODO about #196.

    baf2d91

    Merge branch 'master' into new_generic (Fix #192).

    5ed17e5

    Merge branch 'master' into new_generic (backport #192).

    6a6119c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      • 3.5.2

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.