Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Atomic batteries to power, turbines to speed." -- Robin, The Boy Wonder


devel / comp.lang.python / Re: Typing on child class' methods of a Generic base class

SubjectAuthor
o Re: Typing on child class' methods of a Generic base classDieter Maurer

1
Re: Typing on child class' methods of a Generic base class

<mailman.279.1647109507.2329.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17481&group=comp.lang.python#17481

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: die...@handshake.de (Dieter Maurer)
Newsgroups: comp.lang.python
Subject: Re: Typing on child class' methods of a Generic base class
Date: Sat, 12 Mar 2022 19:24:59 +0100
Lines: 54
Message-ID: <mailman.279.1647109507.2329.python-list@python.org>
References: <3fccbe66-4d1c-f40c-7282-026ebe7360e3@boiteameuh.org>
<25130.13791.689201.429804@ixdm.fritz.box>
<01e56985-280e-f5c3-6bf4-1d7c1a101284@boiteameuh.org>
<25132.58747.285988.346583@ixdm.fritz.box>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de gaFAwlp3C3zEkBK9VWm4fA6MlTdAY8i4K3Co/Dsj7AMQ==
Return-Path: <dieter@handshake.de>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:@ 10': 0.03; 'def':
0.04; 'error:': 0.05; 'variable': 0.05; 'class.': 0.07; 'cc:addr
:python-list': 0.09; 'child': 0.09; 'skip:_ 20': 0.09; 'skip:`
10': 0.09; 'subject:class': 0.09; 'cc:no real name:2**0': 0.14;
'any).': 0.16; 'dieter': 0.16; 'incompatible': 0.16; 'nicolas':
0.16; 'setup,': 0.16; 'skip:> 10': 0.16; 'subject:child': 0.16;
'wrote:': 0.16; 'problem': 0.16; 'says': 0.17;
'cc:addr:python.org': 0.20; 'returns': 0.22; 'received:de': 0.23;
'(and': 0.25; 'saying': 0.25; 'cc:2**0': 0.25; 'so.': 0.26;
'leave': 0.27; '>>>': 0.28; 'example,': 0.28; 'keeping': 0.28;
'error': 0.29; 'seem': 0.31; 'think': 0.32; "doesn't": 0.32;
'context': 0.32; 'but': 0.32; 'header:In-Reply-To:1': 0.34; 'fix':
0.36; 'those': 0.36; 'class': 0.37; 'means': 0.38; 'two': 0.39;
'quite': 0.39; 'use': 0.39; 'methods': 0.39; 'wrote': 0.39;
'base': 0.40; 'should': 0.40; 'follow': 0.62; 'skip:b 10': 0.63;
'key': 0.64; 'specialized': 0.64; 'your': 0.64; 'types': 0.67;
'header:Received:6': 0.67; 'it:': 0.69; 'charset:iso-8859-1':
0.73; 'signature': 0.76; 'received:88': 0.84; 'signatures': 0.84;
'specialize': 0.93
In-Reply-To: <01e56985-280e-f5c3-6bf4-1d7c1a101284@boiteameuh.org>
X-Mailer: VM 8.0.12-devo-585 under 21.4 (patch 24) "Standard C" XEmacs Lucid
(x86_64-linux-gnu)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <25132.58747.285988.346583@ixdm.fritz.box>
X-Mailman-Original-References: <3fccbe66-4d1c-f40c-7282-026ebe7360e3@boiteameuh.org>
<25130.13791.689201.429804@ixdm.fritz.box>
<01e56985-280e-f5c3-6bf4-1d7c1a101284@boiteameuh.org>
 by: Dieter Maurer - Sat, 12 Mar 2022 18:24 UTC

Nicolas Haller wrote at 2022-3-12 12:05 -0500:
>On 2022-03-10 12:31, Dieter Maurer wrote:
>> Nicolas Haller wrote at 2022-3-9 10:53 -0500:
>>> ...
>>> The documentation about "user-defined generic types"[1] says that I can
>>> fix some types on a child class (class MyDict(Mapping[str, T]):) but
>>> doesn't say much about the signature of the methods I need to
>>> implement/override on that child class.
>>
>> I have the fealing that this is a case of (generic type) "specialization".
>> In this setup, `Mapping` would be a generic type with two type
>> variables `K` (the key type) and `V` (the value type).
>> The signatures of its methods would use those type variables.
>> In your example, you specialize the key type to `str` (and
>> leave the value type generic). The signatures of the methods
>> would automatically follow this specialization -- without the need
>> to do anything.
>
>If I understand correctly, you're saying I should use the first
>alternative by keeping the signature as it is in the base class:
>---
>T = TypeVar("T")
>
>
>class Base(Generic[T], metaclass=ABCMeta):
> """A base class."""
>
> @abstractmethod
> def _private_method(self, an_arg: T) -> T:
> ...
>
> def public_method(self, an_arg: T) -> T:
> from_private_method = self._private_method(an_arg)
> return from_private_method
>
>class Alternative1(Base[int]):
> def _private_method(self, an_arg: T) -> T: # I keep T
> return 42
>---
>
>The problem with it is that mypy doesn´t seem quite happy with it:
>./scratch.py:22: error: Incompatible return value type (got "int",
>expected "T")
>
>Do you think this is error is incorrect?

No, I do not think so.

The `Base[int]` means that the generic type variable `T` was
specialized to `int`. This means that in this context
`_private_method` returns `int` (not a generic `T`).

You either do not type annotate the overriding methods
or you use the specialization (if any).

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor