Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Everyone is entitled to an *informed* opinion." -- Harlan Ellison


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

SubjectAuthor
* Typing on child class' methods of a Generic base classNicolas Haller
`- Re: Typing on child class' methods of a Generic base classJulio Di Egidio

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

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

  copy mid

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

  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: Typing on child class' methods of a Generic base class
Date: Wed, 9 Mar 2022 10:53:18 -0500
Lines: 57
Message-ID: <mailman.249.1646846702.2329.python-list@python.org>
References: <3fccbe66-4d1c-f40c-7282-026ebe7360e3@boiteameuh.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de Y2T8/yVJiJ3n2jWOj5GggQQ7Kxitwd1wvZVv9fK8LOyw==
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.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:@ 10': 0.03; 'def':
0.04; 'class.': 0.07; ':-)': 0.09; 'child': 0.09;
'from:addr:python': 0.09; 'skip:_ 20': 0.09; 'subject:class':
0.09; 'import': 0.15; '[1]:': 0.16; 'int)': 0.16; 'involving':
0.16; 'nicolas': 0.16; 'reason.': 0.16; 'received:192.99': 0.16;
'subject:child': 0.16; 'typing': 0.16; 'says': 0.17; 'to:addr
:python-list': 0.20; 'seems': 0.26; 'wrong': 0.28; 'header:User-
Agent:1': 0.30; 'wondering': 0.31; "doesn't": 0.32; 'but': 0.32;
"i'm": 0.33; 'fix': 0.36; 'thanks,': 0.36; 'change': 0.36;
'class': 0.37; 'received:192.168': 0.37; 'methods': 0.39; 'base':
0.40; 'situation': 0.40; 'hello,': 0.40; 'should': 0.40; 'method':
0.61; 'remember': 0.61; 'skip:b 10': 0.63; 'types': 0.67; 'below':
0.69; 'signature': 0.76
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.6.1
Content-Language: en-US
X-Mailman-Approved-At: Wed, 09 Mar 2022 12:25:01 -0500
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: <3fccbe66-4d1c-f40c-7282-026ebe7360e3@boiteameuh.org>
 by: Nicolas Haller - Wed, 9 Mar 2022 15:53 UTC

Hello,

I am wondering about a situation involving typing, Generic and a child
class.

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.

Should I keep the TypeVar and remember that I fixed the type(alternative
1) or change the signature of the method to replace the TypeVar by the
type I set (alternative 2)?

Mypy seems to prefer the second alternative but changing the signature
feels wrong to me for some reason. That's why I'm asking :-)

Below is a small example to illustrate:
----
from abc import ABCMeta, abstractmethod
from typing import TypeVar, Generic

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

class Alternative2(Base[int]):
def _private_method(self, an_arg: int) -> int: # I replace T
return 42
----

Thanks,

--
Nicolas Haller

[1]:
https://docs.python.org/3/library/typing.html#user-defined-generic-types

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

<3228b0ac-1fdc-46ad-91ff-90b17df473f3n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ac8:4e41:0:b0:2e0:6753:cf73 with SMTP id e1-20020ac84e41000000b002e06753cf73mr839603qtw.684.1646849854354;
Wed, 09 Mar 2022 10:17:34 -0800 (PST)
X-Received: by 2002:a05:620a:f14:b0:67d:1b1c:cf34 with SMTP id
v20-20020a05620a0f1400b0067d1b1ccf34mr597891qkl.479.1646849854182; Wed, 09
Mar 2022 10:17:34 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Wed, 9 Mar 2022 10:17:33 -0800 (PST)
In-Reply-To: <mailman.249.1646846702.2329.python-list@python.org>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.97.2; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.97.2
References: <3fccbe66-4d1c-f40c-7282-026ebe7360e3@boiteameuh.org> <mailman.249.1646846702.2329.python-list@python.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3228b0ac-1fdc-46ad-91ff-90b17df473f3n@googlegroups.com>
Subject: Re: Typing on child class' methods of a Generic base class
From: jul...@diegidio.name (Julio Di Egidio)
Injection-Date: Wed, 09 Mar 2022 18:17:34 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 55
 by: Julio Di Egidio - Wed, 9 Mar 2022 18:17 UTC

On Wednesday, 9 March 2022 at 18:26:17 UTC+1, Nicolas Haller wrote:
> Hello,
>
> I am wondering about a situation involving typing, Generic and a child
> class.
>
> 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.
>
> Should I keep the TypeVar and remember that I fixed the type(alternative
> 1) or change the signature of the method to replace the TypeVar by the
> type I set (alternative 2)?
>
> Mypy seems to prefer the second alternative but changing the signature
> feels wrong to me for some reason. That's why I'm asking :-)
>
>
> Below is a small example to illustrate:
> ----
> from abc import ABCMeta, abstractmethod
> from typing import TypeVar, Generic
>
>
> 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
>
> class Alternative2(Base[int]):
> def _private_method(self, an_arg: int) -> int: # I replace T
> return 42

Besides that there is no T in that context, do use int there,
you are "fixing" the type parameter.

BTW, "protected" would be more apt in that case...

HTH,

Julio

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor