Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The University of California Statistics Department; where mean is normal, and deviation standard.


devel / comp.lang.python / Re: Type hints - am I doing it right?

SubjectAuthor
o Re: Type hints - am I doing it right?Cameron Simpson

1
Re: Type hints - am I doing it right?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs...@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: Type hints - am I doing it right?
Date: Wed, 13 Dec 2023 19:47:00 +1100
Lines: 43
Message-ID: <mailman.19.1702457228.2892.python-list@python.org>
References: <66df09f6-0722-471e-ac9b-ca094f3417fb@chagford.com>
<ZXlvhFuHNkUZTtow@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de zkftFdSluV/c0Udk61Kn1wb/4xn0hs479Zpt0vxD78AA==
Cancel-Lock: sha1:WXPwts8ezyIZGYZP1bGx2aenZjQ= sha256:9d0lqDOy0HtUE0tuHnDI2tw5nHyYAQR/eG8oCcas/WQ=
Return-Path: <cameron@cskk.id.au>
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; 'def': 0.04; 'etc).': 0.09;
'module.': 0.09; 'cheers,': 0.11; 'import': 0.15; "'import": 0.16;
'attributes': 0.16; 'base.': 0.16; 'cameron': 0.16; 'database,':
0.16; 'database.': 0.16; 'from:addr:cs': 0.16;
'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson': 0.16;
'hint': 0.16; 'hints': 0.16; 'importing': 0.16; 'message-
id:@cskk.homeip.net': 0.16; 'millman': 0.16; 'modules,': 0.16;
'received:13.237': 0.16; 'received:13.237.201': 0.16;
'received:13.237.201.189': 0.16; 'received:cskk.id.au': 0.16;
'received:id.au': 0.16; 'received:mail.cskk.id.au': 0.16;
'simpson': 0.16; 'subject:Type': 0.16; 'typing': 0.16; 'wrote:':
0.16; 'to:addr:python-list': 0.20; 'way.': 0.22; 'code': 0.23;
'seems': 0.26; 'bit': 0.27; 'etc': 0.28; 'it,': 0.29; 'header
:User-Agent:1': 0.30; 'module': 0.31; 'but': 0.32; 'there': 0.33;
'header:In-Reply-To:1': 0.34; 'received:au': 0.35; 'work,': 0.36;
"it's": 0.37; 'way': 0.38; 'could': 0.38; 'adding': 0.39; 'use':
0.39; 'methods': 0.39; 'method': 0.61; 'imagine': 0.64;
'received:13': 0.64; 'top': 0.65; 'received:userid': 0.66;
'accept': 0.67; 'live': 0.68; 'order': 0.69; 'really.': 0.84;
'receives': 0.91; 'wondered': 0.91
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <66df09f6-0722-471e-ac9b-ca094f3417fb@chagford.com>
User-Agent: Mutt/2.2.7 (2022-08-07)
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: <ZXlvhFuHNkUZTtow@cskk.homeip.net>
X-Mailman-Original-References: <66df09f6-0722-471e-ac9b-ca094f3417fb@chagford.com>
 by: Cameron Simpson - Wed, 13 Dec 2023 08:47 UTC

On 13Dec2023 09:19, Frank Millman <frank@chagford.com> wrote:
>I am adding type hints to my code base.
[...]
>In the other module I have this -
>
>     def config_database(db_params):
>
>To add a type hint, I now have this -
>
>     def config_database(db_params: configparser.SectionProxy):
>
>To get this to work, I have to add 'import configparser' at the top of
>the module.
>
>I have three separate modules, one for each database, with a subclass
>containing the methods and attributes specific to that database. Each
>one has a connect() method which receives db_params as a parameter.
>Now I have to add 'import configparser' at the top of each of these
>modules in order to type hint the method.
>
>This seems verbose. If it is the correct way of doing it I can live
>with it, but I wondered if there was an easier way.

Not really. It's like any other name - it needs importing if you're
going to use it.

You can make the hint itself more compact:

from configparser import SectionProxy
.......
def config_database(db_params: SectionProxy):

Or you could be a bit more agnostic:

from typing import Mapping
.......
def config_database(db_params: Mapping):

since I imagine config_database() would accept any kind of mapping
(dicts, etc etc).

Cheers,
Cameron Simpson <cs@cskk.id.au>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor