Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The world is coming to an end ... SAVE YOUR BUFFERS!!!


devel / comp.lang.python / Re: Type annotation pitfall

SubjectAuthor
* Type annotation pitfallRobert Latest
+- Re: Type annotation pitfallGreg Ewing
+- Re: Type annotation pitfallPeter Saalbrink
`- Re: Type annotation pitfallChris Angelico

1
Type annotation pitfall

<ir5753F2vt1U1@mid.individual.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: boblat...@yahoo.com (Robert Latest)
Newsgroups: comp.lang.python
Subject: Type annotation pitfall
Date: 24 Sep 2021 05:48:19 GMT
Lines: 28
Message-ID: <ir5753F2vt1U1@mid.individual.net>
X-Trace: individual.net CLEe03X4i4Jt18bF6b0XCwpDs9Hj4qhHYtNVGyc200/xJYCLxD
Cancel-Lock: sha1:N14yIJKThD4hXkojW2bFI/o29Mc=
User-Agent: slrn/1.0.3 (Linux)
 by: Robert Latest - Fri, 24 Sep 2021 05:48 UTC

Hi all,

this just caused me several hours of my life until I could whittle it down to
this minimal example. Simple question: Why is the x member of object "foo"
modified by initializing "bar"?

Obviously, initializing foo with None doesn't set foo.x at all. So I guess x
stays a class property, not an instance property. And instantiating bar just
added an item to the class property but didn't instantiate a new set. So
basically, neither foo nor bar ever had their "own" x, right?

Oooohh, dangerous! Never use mutable types in type hint, unless it's in
explicit dataclasses (which automatically add an appropriate __init__()?)

Now I must fine-comb all my code for more of these.

class Foo():
x : set = set()

def __init__(self, s):
if s:
self.x.add(s)

foo = Foo(None)
print(foo.x) # prints 'set()'
bar = Foo('abc')
print(foo.x) # prints '{'abc'}

Re: Type annotation pitfall

<ir5990F3f7aU1@mid.individual.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: greg.ew...@canterbury.ac.nz (Greg Ewing)
Newsgroups: comp.lang.python
Subject: Re: Type annotation pitfall
Date: Fri, 24 Sep 2021 18:24:29 +1200
Lines: 19
Message-ID: <ir5990F3f7aU1@mid.individual.net>
References: <ir5753F2vt1U1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net WTahnhcv5i5fWnhacIBhzwxE5chKWzI5nBewhHSZpQN1NgcjAv
Cancel-Lock: sha1:5AeUNw7ovdcuQk6i1HRWkomByx4=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:78.0)
Gecko/20100101 Thunderbird/78.4.0
In-Reply-To: <ir5753F2vt1U1@mid.individual.net>
Content-Language: en-US
 by: Greg Ewing - Fri, 24 Sep 2021 06:24 UTC

On 24/09/21 5:48 pm, Robert Latest wrote:
> Never use mutable types in type hint,

No, the lesson is: Don't mutate a shared object if you don't want
the changes to be shared.

If you want each instance to have its own set object, you need to
create one for it in the __init__ method, e.g.

class Foo():
x : set

def __init__(self, s):
self.x = set()
if s:
self.x.add(s)

--
Greg

Re: Type annotation pitfall

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: petersaa...@gmail.com (Peter Saalbrink)
Newsgroups: comp.lang.python
Subject: Re: Type annotation pitfall
Date: Fri, 24 Sep 2021 13:28:29 +0200
Lines: 67
Message-ID: <mailman.790.1632490905.4164.python-list@python.org>
References: <ir5753F2vt1U1@mid.individual.net>
<CAN4UqHjNXBPMRdvaBGjc9HeRkngbbvhyc5yAgfEq3M+x5nN7dg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de N0Y8JzcVubgIKHA6pBN6GAv0lZXrl98cXyXG8bm16esQ==
Return-Path: <petersaalbrink@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=kr44hNJf;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(which': 0.04; 'def':
0.04; 'class.': 0.07; 'cc:addr:python-list': 0.09; 'email
addr:python.org>': 0.09; 'example.': 0.09; 'instances': 0.09;
'obviously,': 0.09; 'prints': 0.09; 'question:': 0.09; 'set.':
0.09; 'skip:` 10': 0.09; 'cc:no real name:2**0': 0.14;
'url:mailman': 0.15; 'declare': 0.16; 'explicit': 0.16; 'hint':
0.16; 'instance': 0.16; 'part.': 0.16; 'subject:Type': 0.16;
'these.': 0.16; 'typing': 0.16; 'variable.': 0.16; 'wrote:': 0.16;
'cc:addr:python.org': 0.20; 'all,': 0.20; 'fri,': 0.22; 'skip:_
10': 0.22; 'code': 0.23; 'idea': 0.24; 'anything': 0.25; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'cc:2**0': 0.25; 'url-ip:188.166/16': 0.25;
'object': 0.26; 'robert': 0.26; 'else': 0.27; 'email
addr:python.org&gt;': 0.28; 'url-ip:188/8': 0.31; 'think': 0.32;
"doesn't": 0.32; 'guess': 0.32; 'here,': 0.32; 'modified': 0.32;
'python-list': 0.32; 'said,': 0.32; 'sep': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'unless': 0.32; 'but': 0.32; "didn't":
0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'bar': 0.35; 'item': 0.35; 'following': 0.35;
'from:addr:gmail.com': 0.35; 'member': 0.37; "it's": 0.37;
'received:209.85': 0.37; 'class': 0.37; 'could': 0.38;
'received:209': 0.39; 'added': 0.39; 'use': 0.39; 'neither': 0.39;
'shared': 0.39; 'provide': 0.60; 'ever': 0.63; 'hours': 0.63;
'providing': 0.63; 'down': 0.64; 'to:addr:yahoo.com': 0.64;
'8bit%:7': 0.67; 'types': 0.67; '2021': 0.71; 'life': 0.77;
'attribute': 0.84; 'email name:&lt;python-list': 0.84; 'caused':
0.86; 'property': 0.88; 'safely': 0.91; 'stays': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc; bh=DvqjjRPxJ+8ZC//MaQrZ0PqfZybnkJY3BS3fFm4rnU8=;
b=kr44hNJfg5YKtQAyhXFXULgBZeF91UBDDHWnMn55dPf9TXwjoZM5yRMe4o0ru68mHn
uSeFM0az3FUDxhZT3Ima3bHVNbj9BWngnxobLURjvj0mzigPNj8/aImtt1EI9bh8zTVR
JHbNtETl5tEfc3DmbOOzeUz5mdMH+KDeAKI1ScfQ1ffiZ/yaXc2h5QZzYRYRirBA8cBM
54Ai3r/oOwH+CflxKB2BhIYgQWIlCBe5L2pD/gglXytX+M5wE7oAAtYhQT0uoikncqjw
i/16LlNcAB35zOmIAV1EqQ1q6pg6eSD6/JvZLRrE8GhrcL5NnFiUOS7usHmModoDtuQg
9yBg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to:cc;
bh=DvqjjRPxJ+8ZC//MaQrZ0PqfZybnkJY3BS3fFm4rnU8=;
b=yvqjx+XwlqZWu1W+aXaVahsa9eRo9nhgJUH7LCtGeVUXheOpo+0c0m24NfO2hS1WMc
LJY5DW/QoRmcA3Eiq2VYdhaeYXbC3PHcBRzAyrUqLRSvYzPTVHiJuIEDmPsNM1dQnEB8
SMf8sgj2wgV5cB0xKHo9x1v45V3MB2TKt1WFtGF/A4qeGy0TOoLg4lhSiCS7M/ceayu/
6JhXdbP0eAuzPwuhjLjdcQFySw5Jp0B+551cx1qddudfFI/37Mvhuyza4g6KAy1jJDoP
iboeNoPs+9MP86Tyr/fZFKAXAdAcDGUWca5mEnd43dsBwi62UWImCr2AlIny9/j7FN00
jmDg==
X-Gm-Message-State: AOAM531h1rTX1FxzwTEHjh784YdYDY2jwz6UBFJ5rKc7LW4kHqxAS8IH
uU8jgkTNbHciGxoigMoD2IcDMRB7T2cNWnvDOe/6ug2h/NI=
X-Google-Smtp-Source: ABdhPJxvIZ/ZV3MoMyEhbuUICk3SDCdDA9zj4PXLLMXHCrah50Cer89NsE1leZF3V3xxGfgRFx4nheUkw5hqrkLElKI=
X-Received: by 2002:a19:5051:: with SMTP id z17mr8809149lfj.313.1632482921004;
Fri, 24 Sep 2021 04:28:41 -0700 (PDT)
In-Reply-To: <ir5753F2vt1U1@mid.individual.net>
X-Mailman-Approved-At: Fri, 24 Sep 2021 09:41:43 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.34
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
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: <CAN4UqHjNXBPMRdvaBGjc9HeRkngbbvhyc5yAgfEq3M+x5nN7dg@mail.gmail.com>
X-Mailman-Original-References: <ir5753F2vt1U1@mid.individual.net>
 by: Peter Saalbrink - Fri, 24 Sep 2021 11:28 UTC

I don't think this has anything to do with typing or providing type hints.
The type hint is the `: set` part, not the `= set()` part.
You can declare the type without assigning to the variable.
Indeed, as you already said, `x` is a class property here, and is shared
amongst instances of the class.
It might be a good idea to move the attribute assignment to the `__init__`
method.

In the following way, you can safely provide the type hint:

```python
class Foo:
x: set

def __init__(self, s):
self.x = set()
if s:
self.x.add(s)
```

Or, even shorter:

```python
class Foo:
def __init__(self, s: str):
self.x: set[str] = {s} if s else set()

print(reveal_type(Foo.x)) # mypy only
```

On Fri, Sep 24, 2021 at 7:58 AM Robert Latest via Python-list <
python-list@python.org> wrote:

> Hi all,
>
> this just caused me several hours of my life until I could whittle it down
> to
> this minimal example. Simple question: Why is the x member of object "foo"
> modified by initializing "bar"?
>
> Obviously, initializing foo with None doesn't set foo.x at all. So I guess
> x
> stays a class property, not an instance property. And instantiating bar
> just
> added an item to the class property but didn't instantiate a new set. So
> basically, neither foo nor bar ever had their "own" x, right?
>
> Oooohh, dangerous! Never use mutable types in type hint, unless it's in
> explicit dataclasses (which automatically add an appropriate __init__()?)
>
> Now I must fine-comb all my code for more of these.
>
> class Foo():
> x : set = set()
>
> def __init__(self, s):
> if s:
> self.x.add(s)
>
> foo = Foo(None)
> print(foo.x) # prints 'set()'
> bar = Foo('abc')
> print(foo.x) # prints '{'abc'}
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Re: Type annotation pitfall

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Type annotation pitfall
Date: Fri, 24 Sep 2021 23:47:53 +1000
Lines: 33
Message-ID: <mailman.791.1632491289.4164.python-list@python.org>
References: <ir5753F2vt1U1@mid.individual.net>
<CAN4UqHjNXBPMRdvaBGjc9HeRkngbbvhyc5yAgfEq3M+x5nN7dg@mail.gmail.com>
<CAPTjJmr+p5t0PUYREJUoPpQmc3XZM4K_h8ivcqpnD00uQYcPXQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de cQ4/vcgl9l3iTqCa10QplA22eGgdZLCbGUNPYV1if+2g==
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=gkIeXpeN;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; 'class.':
0.07; 'construct': 0.09; 'instances': 0.09; 'set,': 0.09; '(so':
0.16; 'chrisa': 0.16; 'declare': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; 'hint': 0.16; 'instance': 0.16;
'part.': 0.16; 'subject:Type': 0.16; 'typing': 0.16; 'variable.':
0.16; 'wrote:': 0.16; 'to:addr:python-list': 0.20; 'fri,': 0.22;
'skip:_ 10': 0.22; 'idea': 0.24; 'anything': 0.25; 'object': 0.26;
"isn't": 0.27; 'think': 0.32; 'attach': 0.32; 'here,': 0.32;
'said,': 0.32; 'sep': 0.32; 'to:name:python': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'same': 0.34; 'header:In-Reply-To:1':
0.34; 'received:google.com': 0.34; 'following': 0.35;
'received:209.85.166': 0.35; 'from:addr:gmail.com': 0.35;
'received:209.85': 0.37; 'class': 0.37; 'received:209': 0.39;
'single': 0.39; 'use': 0.39; 'neither': 0.39; 'shared': 0.39;
'want': 0.40; 'provide': 0.60; 'providing': 0.63; 'among': 0.65;
'2021': 0.71; 'attribute': 0.84; 'property': 0.88; 'safely': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=qEUx/wecM1lT0i9u/wGaeIZIFUUhfKkafwGNKJfixwY=;
b=gkIeXpeNktEAyCuwFg2Qaiv4OSHvUNL6L6BmoSdE7ss+iOcDElJxsodoDxO324rIhu
C1W5tqqy+eL0PO2yjsh6px4wZGuA3yB6DPFXe6QQtTu4Aj8ey2H5StsGqODeCYp04eL/
TOBCb+I9cIgMrHkFYOlxHPKR1xBTRQ/rGW16pN1lQL+z+I1ZWZ6fpXDUJcGRE36zzhwq
azzNIZmJbnVh7Shr1nkiYaj00k6gnOQiLvjVPMDrqqBoPwbasZTeqto6Y3yyuT5XDazz
ZMhcslqGaNWSfCixGLDM9gMgdsv2b4wUqLdVttoi8HCNcCIg9CHT5qDLKFH0tD0/2WyJ
MC5A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=qEUx/wecM1lT0i9u/wGaeIZIFUUhfKkafwGNKJfixwY=;
b=nclPWzJu3hqVr6fm0HoKbRdQUbebD2Go3r8LL1swzovw374XDrAgFwKo0j3fIP+lji
L5eMOCufGaLDaSy9FU5ITWBx0TPov3PDnDt8IhyG5HIfxXlYoKR/IPc2K+nikqHGlJzQ
6TjKdOGFeZUb5f1zaZhj/2ZJOAGDt07nkWcbsP5+PhG8ESOaKvelE3/l5cTUIFV5scBA
skD9bhdjV1qqVwQftrazJMVnWRhML6QBYg+lNfPJySFb8lt08MvrMhVr+lLZOyziQLRn
6t1c1gVqXIem8p+rWMZq3fHNASi4tjDDmiGhsBSxVxh1rf/lcSMNR7QOULKacFsJiCv2
JquA==
X-Gm-Message-State: AOAM531tMu+fy5yHX6RaV0lLmUoLdOfH+GtYLgu5lfNnZ6G92cPrgzXt
jr4L4E6IvSxAc/aRfazx27dO08f2aF3oiyf5VzM06VK3oLY=
X-Google-Smtp-Source: ABdhPJy43HFF/naWvsbnfVb0KK6p+erfI6lGbHUSTa2rWWfGcz2QHfkecsaFS6bJAOvobwWZD2WO2IgRhjRBa6lEWkA=
X-Received: by 2002:a05:6e02:d8c:: with SMTP id
i12mr7930744ilj.190.1632491284929;
Fri, 24 Sep 2021 06:48:04 -0700 (PDT)
In-Reply-To: <CAN4UqHjNXBPMRdvaBGjc9HeRkngbbvhyc5yAgfEq3M+x5nN7dg@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
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: <CAPTjJmr+p5t0PUYREJUoPpQmc3XZM4K_h8ivcqpnD00uQYcPXQ@mail.gmail.com>
X-Mailman-Original-References: <ir5753F2vt1U1@mid.individual.net>
<CAN4UqHjNXBPMRdvaBGjc9HeRkngbbvhyc5yAgfEq3M+x5nN7dg@mail.gmail.com>
 by: Chris Angelico - Fri, 24 Sep 2021 13:47 UTC

On Fri, Sep 24, 2021 at 11:43 PM Peter Saalbrink
<petersaalbrink@gmail.com> wrote:
>
> I don't think this has anything to do with typing or providing type hints.
> The type hint is the `: set` part, not the `= set()` part.
> You can declare the type without assigning to the variable.
> Indeed, as you already said, `x` is a class property here, and is shared
> amongst instances of the class.
> It might be a good idea to move the attribute assignment to the `__init__`
> method.
>
> In the following way, you can safely provide the type hint:
>
> ```python
> class Foo:
> x: set
>
> def __init__(self, s):
> self.x = set()
> if s:
> self.x.add(s)
> ```
>

To be clear, this isn't a case of "never use mutables as class
attributes"; often you *want* a single mutable object to be shared
among instances of a class (so they can all find each other, perhaps).
If you want each instance to have its own set, you construct a new set
every object initialization; if you want them to all use the same set,
you construct a single set and attach it to the class. Neither is
wrong, they just have different meanings.

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor