Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Apples have meant trouble since eden. -- MaDsen Wikholm, mwikholm@at8.abo.fi


devel / comp.lang.python / Re: type annotation vs working code

SubjectAuthor
o Re: type annotation vs working codeChris Angelico

1
Re: type annotation vs working code

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.chmurka.net!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: type annotation vs working code
Date: Sun, 1 Oct 2023 23:21:00 +1100
Lines: 54
Message-ID: <mailman.377.1696162874.23016.python-list@python.org>
References: <ZRhwWN29i3mskxEL@hermes.hilbert.loc>
<ecc36615-1844-455f-b701-2bd41d98e9e1@DancesWithMice.info>
<ZRigRwHpNReUo5rF@hermes.hilbert.loc>
<4f1c9568-3850-4847-b357-740f79b0a79b@DancesWithMice.info>
<ZRlelmUwOR/wcPqR@hermes.hilbert.loc>
<CAPTjJmpZ_gAajMN-7GKPTKD4tqtHeJ6WrtOOdVnSn+H9h-AZGA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de V+o/FbJfpbBa3MRfjiAWVQcx/12g5xXhXsB+leihy/Dg==
Cancel-Lock: sha1:sNm3yhmyDwGK0L6+TwjsCEPVa6A= sha256:/3f7PW5Gpwl9DIpduU3OxmZ/yzxgxSZYCLhT9vuL5j0=
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=Qe2bVjDq;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'def': 0.04;
'2023': 0.07; 'sun,': 0.07; ':-)': 0.09; 'borg': 0.09;
'expression': 0.09; 'karsten': 0.09; 'url-ip:151.101.0.223/32':
0.09; 'url-ip:151.101.128.223/32': 0.09; 'url-
ip:151.101.192.223/32': 0.09; 'url-ip:151.101.64.223/32': 0.09;
'annotated': 0.16; 'attributes': 0.16; 'chrisa': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'hilbert': 0.16; 'hints': 0.16; 'inherently': 0.16; 'instance':
0.16; 'level,': 0.16; 'received:mail-lf1-x131.google.com': 0.16;
'subject:code': 0.16; 'subject:working': 0.16; 'typing': 0.16;
'url:peps': 0.16; 'wrote:': 0.16; 'problem': 0.16; 'python': 0.16;
'code.': 0.17; 'uses': 0.19; 'to:addr:python-list': 0.20; "i've":
0.22; 'basically': 0.22; 'skip:_ 10': 0.22; 'code': 0.23; 'idea':
0.24; 'python,': 0.25; 'normally': 0.26; "isn't": 0.27; 'code,':
0.31; 'python-list': 0.32; 'message-id:@mail.gmail.com': 0.32;
'there': 0.33; 'able': 0.34; 'core': 0.34; "didn't": 0.34; 'skip:"
20': 0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'particularly': 0.35; 'from:addr:gmail.com': 0.35; "it's":
0.37; 'class': 0.37; 'two': 0.39; 'changes': 0.39; 'quite': 0.39;
'adding': 0.39; 'rest': 0.39; 'seeing': 0.39; 'should': 0.40;
'sorry': 0.60; 'here.': 0.61; 'url-ip:151.101.0/24': 0.62; 'url-
ip:151.101.128/24': 0.62; 'url-ip:151.101.192/24': 0.62; 'url-
ip:151.101.64/24': 0.62; 'simply': 0.63; 'your': 0.64; 'improve':
0.66; 'nearly': 0.67; 'matter': 0.68; 'skip:w 20': 0.69;
'instead,': 0.70; 'supposed': 0.76; 'significant': 0.78;
'apparent': 0.84; 'conflated': 0.84; 'subject:type': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1696162872; x=1696767672; darn=python.org;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=PM3ZzaLjxNUUh2V1krHzRsIrxiDoe2hz3QwFj0DhZY0=;
b=Qe2bVjDqobn25Jgu6XqxXLeTp6KG3q+oufV1dOuoah5ISSdImw15aNncHMWGx4HuP8
Q4WqyB56GQe887uGtw9UyPEwCa7r9bWQhmnk1iwX6RP1PNJVcRVE6qpqPBIce6Z+cGIE
ODlEWZsybsvH5QT4ED3Ipvg1yrna+QVTizRPvye7HsGtIdPvltvq381BDU8UlHW+CAML
eCvytkAKMHt6HfSThpGsr0jswjBH7r/lIN1vpMGoSH4Qkl1hil93KdCNaUgdsFjlDZ7m
npF7+oEKR/qKA22/GMgJPPurquQcxR3RfP+3hg8aJ/M4YZxT3NZD60BsNLn7MV7YDn6i
G8Ww==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1696162872; x=1696767672;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=PM3ZzaLjxNUUh2V1krHzRsIrxiDoe2hz3QwFj0DhZY0=;
b=tC2B7YSrSzC9aIp7nMnUud+mSHO60Zz0kCAQ7vqnby1kkxIUZnNSlsPl+hfWcoxwSy
ut/JqE2a+Hyht6kd+CCo9QhSwbg2ksarMjNKkKiQM45wpbkWz4x27s0X8PLQn6pCD35w
slYI2fQqOXh/84wffMY4+efR/7ZD7y/rsDZH4wRYZ2svvsV1TBknPXIu/wG/Uj+M/ItZ
vjRifwzUBys/zq56zF1CdqT2Yr6/GA834+9/tYAY8X7oQ7Q0B3aAcYlDX8Yza6jET9eR
kVic4G6x96DB0ITjLHa5EXS2six6GWY/IsziFYFYYuoE8EeU10252tzDL5VWTuxffjaY
NQ1g==
X-Gm-Message-State: AOJu0YwOtUCocuxTJ8kCcX5URNxicY5IXhmlSwu9wLNkZcVyRow76U+z
jVWScQq2M6Vfu8EDSFVjuk4gW8+kkXqSRQwCxGm54kJo3PE=
X-Google-Smtp-Source: AGHT+IHNtrr35RHHkDZtmE0MThWro/ezBtk9nK0pVdpzMTZJ7lwr8CLZfhxxwqa4hcNyTrBeslLHfHk1jv1Z71ao7ko=
X-Received: by 2002:a05:6512:33cf:b0:500:bbd4:970f with SMTP id
d15-20020a05651233cf00b00500bbd4970fmr8737430lfg.5.1696162872066; Sun, 01 Oct
2023 05:21:12 -0700 (PDT)
In-Reply-To: <ZRlelmUwOR/wcPqR@hermes.hilbert.loc>
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: <CAPTjJmpZ_gAajMN-7GKPTKD4tqtHeJ6WrtOOdVnSn+H9h-AZGA@mail.gmail.com>
X-Mailman-Original-References: <ZRhwWN29i3mskxEL@hermes.hilbert.loc>
<ecc36615-1844-455f-b701-2bd41d98e9e1@DancesWithMice.info>
<ZRigRwHpNReUo5rF@hermes.hilbert.loc>
<4f1c9568-3850-4847-b357-740f79b0a79b@DancesWithMice.info>
<ZRlelmUwOR/wcPqR@hermes.hilbert.loc>
 by: Chris Angelico - Sun, 1 Oct 2023 12:21 UTC

On Sun, 1 Oct 2023 at 22:58, Karsten Hilbert via Python-list
<python-list@python.org> wrote:
>
> Sorry for having conflated the core of the matter with all
> the Borg shenanigans, that's where I found the problem in my
> real code, so there :-)
>
> Consider this:
>
> #----------------------------------------------------
> class Surprise:
> def __init__(self, with_type_annotation=False):
> if with_type_annotation:
> try:
> self.does_not_exist:bool
> print('does_not_exist does exist')
> except AttributeError:
> print('does_not_exist does not exist')
> return
>
> try:
> self.does_not_exist
> print('does_not_exist does exist')
> except AttributeError:
> print('does_not_exist does not exist')
>
> Surprise(with_type_annotation = False)
> Surprise(with_type_annotation = True)
> #----------------------------------------------------
>
> Is this how it is supposed to be ?

The class isn't even significant here. What you're seeing is simply
that an annotation does not evaluate the expression.

https://peps.python.org/pep-0526/

It's basically a coincidence that your two versions appear nearly
identical. They are quite different semantically. Note that annotating
the expression "self.does_not_exist" is not particularly meaningful to
Python, and I've no idea what different type checkers will do with it;
you normally only annotate variables that you own - so, in a function,
that's function-local variables. Instead, class and instance
attributes should be annotated at the class level, which would remove
this apparent similarity.

This is a very good reason NOT to arbitrarily add type hints to code.
Type hints do not inherently improve code, and making changes just for
the sake of adding them risks making semantic changes that you didn't
intend. Python uses a system of gradual typing for very good reason;
you should be able to add hints only to the places where they're
actually useful, leaving the rest of the code untouched.

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor