Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Life is a whim of several billion cells to be you for a while.


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 classNicolas Haller

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: pyt...@boiteameuh.org (Nicolas Haller)
Newsgroups: comp.lang.python
Subject: Re: Typing on child class' methods of a Generic base class
Date: Sat, 12 Mar 2022 12:05:43 -0500
Lines: 50
Message-ID: <mailman.278.1647104751.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>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de Cip5z/EGs45eNJ9UqBwCFwcfakHSh1+0e3ihGkYTmWEw==
Return-Path: <python@boiteameuh.org>
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; 'class.': 0.07; 'cc:addr:python-list': 0.09;
'child': 0.09; 'from:addr:python': 0.09; 'skip:_ 20': 0.09;
'subject:class': 0.09; 'cc:no real name:2**0': 0.14; 'dieter':
0.16; 'incompatible': 0.16; 'nicolas': 0.16; 'received:192.99':
0.16; 'setup,': 0.16; 'subject:child': 0.16; 'wrote:': 0.16;
'problem': 0.16; 'says': 0.17; 'cc:addr:python.org': 0.20;
'issue': 0.21; '(and': 0.25; 'saying': 0.25; 'cc:2**0': 0.25;
'leave': 0.27; 'example,': 0.28; 'keeping': 0.28; 'error': 0.29;
'header:User-Agent:1': 0.30; 'seem': 0.31; 'think': 0.32;
"doesn't": 0.32; 'but': 0.32; 'header:In-Reply-To:1': 0.34; 'fix':
0.36; 'thanks,': 0.36; 'those': 0.36; 'class': 0.37;
'received:192.168': 0.37; '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; 'your': 0.64;
'types': 0.67; 'it:': 0.69; 'signature': 0.76; 'signatures': 0.84;
'specialize': 0.93
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.6.1
Content-Language: en-US
In-Reply-To: <25130.13791.689201.429804@ixdm.fritz.box>
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: <01e56985-280e-f5c3-6bf4-1d7c1a101284@boiteameuh.org>
X-Mailman-Original-References: <3fccbe66-4d1c-f40c-7282-026ebe7360e3@boiteameuh.org>
<25130.13791.689201.429804@ixdm.fritz.box>
 by: Nicolas Haller - Sat, 12 Mar 2022 17:05 UTC

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? If so, I can open an issue with
mypy.

Thanks,

--
Nicolas

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor