Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Never ascribe to malice that which is caused by greed and ignorance." -- Cal Keegan


devel / comp.lang.python / Typing: Is there a "cast operator"?

SubjectAuthor
* Typing: Is there a "cast operator"?Paulo da Silva
+- Re: Typing: Is there a "cast operator"? [RESOLVED]Paulo da Silva
+* Re: Typing: Is there a "cast operator"?Cameron Simpson
|`- Re: Typing: Is there a "cast operator"?Paulo da Silva
+- Re: Typing: Is there a "cast operator"?Dan Stromberg
+- Re: Typing: Is there a "cast operator"?Chris Angelico
+- Re: Typing: Is there a "cast operator"?Thomas Passin
`- Re: Typing: Is there a "cast operator"?Peter Otten

1
Typing: Is there a "cast operator"?

<tj48k6$ndp$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!r2jfXpftVyFE0Vzaeaalag.user.46.165.242.75.POSTED!not-for-mail
From: p_d_a_s_...@nonetnoaddress.pt (Paulo da Silva)
Newsgroups: comp.lang.python
Subject: Typing: Is there a "cast operator"?
Date: Sun, 23 Oct 2022 21:36:21 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tj48k6$ndp$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="23993"; posting-host="r2jfXpftVyFE0Vzaeaalag.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Firefox/102.0 Thunderbird/102.2.2
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: pt-PT, en-US
 by: Paulo da Silva - Sun, 23 Oct 2022 20:36 UTC

Hello!

I am in the process of "typing" of some of my scripts.
Using it should help a lot to avoid some errors.
But this is new for me and I'm facing some problems.

Let's I have the following code (please don't look at the program content):

f=None # mypy naturally assumes Optional(int) because later, at open,
it is assigned an int.
...
if f is None:
f=os.open(...
...
if f is not None:
os.write(f, ...)
...
if f is not None:
os.close(f)

When I use mypy, it claims
Argument 1 to "write" has incompatible type "Optional[int]"; expected "int"
Argument 1 to "close" has incompatible type "Optional[int]"; expected "int"

How to solve this?
Is there a way to specify that when calling os.open f is an int only?

I use None a lot for specify uninitialized vars.

Thank you.

Re: Typing: Is there a "cast operator"? [RESOLVED]

<tj4c47$3q4$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!r2jfXpftVyFE0Vzaeaalag.user.46.165.242.75.POSTED!not-for-mail
From: p_d_a_s_...@nonetnoaddress.pt (Paulo da Silva)
Newsgroups: comp.lang.python
Subject: Re: Typing: Is there a "cast operator"? [RESOLVED]
Date: Sun, 23 Oct 2022 22:36:07 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tj4c47$3q4$1@gioia.aioe.org>
References: <tj48k6$ndp$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="3908"; posting-host="r2jfXpftVyFE0Vzaeaalag.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Firefox/102.0 Thunderbird/102.2.2
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: pt-PT, en-US
 by: Paulo da Silva - Sun, 23 Oct 2022 21:36 UTC

Às 21:36 de 23/10/22, Paulo da Silva escreveu:
> Hello!
>
> I am in the process of "typing" of some of my scripts.
> Using it should help a lot to avoid some errors.
> But this is new for me and I'm facing some problems.
>
> Let's I have the following code (please don't look at the program content):
>
> f=None  # mypy naturally assumes Optional(int) because later, at open,
> it is assigned an int.
> ..
> if f is None:
>     f=os.open(...
> ..
> if f is not None:
>     os.write(f, ...)
> ..
> if f is not None:
>     os.close(f)
>
> When I use mypy, it claims
> Argument 1 to "write" has incompatible type "Optional[int]"; expected "int"
> Argument 1 to "close" has incompatible type "Optional[int]"; expected "int"
>
> How to solve this?
> Is there a way to specify that when calling os.open f is an int only?
And yes there is! Exactly the "cast" operator. A mistype led me to wrong
search results. I'm sorry.

So, in the above code, we have to do:
os.write(cast(int,f), ...)
and
os.close(cast(int,f), ...)

Regards.
Paulo

Re: Typing: Is there a "cast operator"?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs...@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: Typing: Is there a "cast operator"?
Date: Mon, 24 Oct 2022 09:56:07 +1100
Lines: 55
Message-ID: <mailman.775.1666565779.20444.python-list@python.org>
References: <tj48k6$ndp$1@gioia.aioe.org>
<Y1XGhw9kRADFxD2t@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
X-Trace: news.uni-berlin.de wWoMwSNIroK7yY9YU9YdIwM2cbc1ASW8sBYJ4SSeaWJw==
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.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'pfxlen:0': 0.03;
'traceback': 0.04; '(for': 0.05; '(most': 0.05; 'last):': 0.05;
'assumes': 0.07; 'aug': 0.07; 'scripts.': 0.07; 'cc:addr:python-
list': 0.09; 'example:': 0.09; 'int': 0.09; 'later,': 0.09;
'cheers,': 0.11; 'cc:no real name:2**0': 0.14; '(b)': 0.16;
'2022,': 0.16; 'bugs': 0.16; 'cameron': 0.16; 'from:addr:cs':
0.16; 'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson':
0.16; 'incompatible': 0.16; 'intent': 0.16; 'message-
id:@cskk.homeip.net': 0.16; 'none:': 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:l': 0.16; 'received:mail.cskk.id.au': 0.16; 'simpson':
0.16; 'specify': 0.16; 'wrote:': 0.16; 'python': 0.16; 'probably':
0.17; 'solve': 0.19; 'cc:addr:python.org': 0.20; 'maybe': 0.22;
'code': 0.23; 'cc:2**0': 0.25; '>>>': 0.28; 'this?': 0.29;
'error': 0.29; 'header:User-Agent:1': 0.30; 'whole': 0.30;
'program': 0.31; '"",': 0.32; 'checks': 0.32; 'subject:there':
0.32; "i'm": 0.33; 'there': 0.33; 'header:In-Reply-To:1': 0.34;
'definitely': 0.35; 'received:au': 0.35; 'following': 0.35;
'file': 0.38; 'way': 0.38; 'could': 0.38; 'use': 0.39; 'should':
0.40; 'skip:o 10': 0.61; 'pass': 0.64; 'claims': 0.64;
'received:13': 0.64; 'your': 0.64; 'similar': 0.65; 'look': 0.65;
'received:userid': 0.66; 'types': 0.67; 'front': 0.70;
'subsequent': 0.76; 'var': 0.81; '(please': 0.84; 'naturally':
0.84; 'stating': 0.84; 'type,': 0.84; 'type.': 0.84
Mail-Followup-To: Paulo da Silva <p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt>,
python-list@python.org
Content-Disposition: inline
In-Reply-To: <tj48k6$ndp$1@gioia.aioe.org>
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: <Y1XGhw9kRADFxD2t@cskk.homeip.net>
X-Mailman-Original-References: <tj48k6$ndp$1@gioia.aioe.org>
 by: Cameron Simpson - Sun, 23 Oct 2022 22:56 UTC

On 23Oct2022 21:36, Paulo da Silva <p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt> wrote:
>I am in the process of "typing" of some of my scripts.
>Using it should help a lot to avoid some errors.
>But this is new for me and I'm facing some problems.
>
>Let's I have the following code (please don't look at the program content):
>
>f=None # mypy naturally assumes Optional(int) because later, at open,
>it is assigned an int.
>..
>if f is None:
> f=os.open(...
>..
>if f is not None:
> os.write(f, ...)
>..
>if f is not None:
> os.close(f)
>
>When I use mypy, it claims
>Argument 1 to "write" has incompatible type "Optional[int]"; expected "int"
>Argument 1 to "close" has incompatible type "Optional[int]"; expected "int"
>
>How to solve this?
>Is there a way to specify that when calling os.open f is an int only?
>
>I use None a lot for specify uninitialized vars.

Maybe you shouldn't. The other way is to just not initialise the var at
all. You could then just specify a type. Example:

Python 3.8.13 (default, Aug 11 2022, 15:46:53)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more
information.
>>> f:int
>>> f
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'f' is not defined
>>>

So now `f` has `int` type definitely (for `mypy`'s purposes), and if
used before assignment raises a distinctive error (versus having the
value `None`, which you might then pass around, and perhaps successfully
use in some contexts).

It is probably better on the whole to specify types up front rather than
relying on `mypy` or similar to infer them. That way (a) you're stating
your intent and (b) not relying on an inferred type, which if you've got
bugs may be inferred _wrong_. If `mypy` infers a type incorrectly all
the subsequent checks will also be flawed, perhaps subtly.

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

Re: Typing: Is there a "cast operator"?

<tj4qoa$jsd$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!r2jfXpftVyFE0Vzaeaalag.user.46.165.242.75.POSTED!not-for-mail
From: p_d_a_s_...@nonetnoaddress.pt (Paulo da Silva)
Newsgroups: comp.lang.python
Subject: Re: Typing: Is there a "cast operator"?
Date: Mon, 24 Oct 2022 02:45:45 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tj4qoa$jsd$1@gioia.aioe.org>
References: <tj48k6$ndp$1@gioia.aioe.org> <Y1XGhw9kRADFxD2t@cskk.homeip.net>
<mailman.775.1666565779.20444.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="20365"; posting-host="r2jfXpftVyFE0Vzaeaalag.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Firefox/102.0 Thunderbird/102.2.2
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: pt-PT, en-US
 by: Paulo da Silva - Mon, 24 Oct 2022 01:45 UTC

Às 23:56 de 23/10/22, Cameron Simpson escreveu:
> On 23Oct2022 21:36, Paulo da Silva
> <p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt> wrote:
>> I am in the process of "typing" of some of my scripts.
>> Using it should help a lot to avoid some errors.
>> But this is new for me and I'm facing some problems.
>>
>> Let's I have the following code (please don't look at the program
>> content):
>>
>> f=None  # mypy naturally assumes Optional(int) because later, at open,
>> it is assigned an int.
>> ..
>> if f is None:
>>     f=os.open(...
>> ..
>> if f is not None:
>>     os.write(f, ...)
>> ..
>> if f is not None:
>>     os.close(f)
>>
>> When I use mypy, it claims
>> Argument 1 to "write" has incompatible type "Optional[int]"; expected
>> "int"
>> Argument 1 to "close" has incompatible type "Optional[int]"; expected
>> "int"
>>
>> How to solve this?
>> Is there a way to specify that when calling os.open f is an int only?
>>
>> I use None a lot for specify uninitialized vars.
>
> Maybe you shouldn't. The other way is to just not initialise the var at
> all. You could then just specify a type. Example:
>
>    Python 3.8.13 (default, Aug 11 2022, 15:46:53)
>    [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
>    Type "help", "copyright", "credits" or "license" for more
> information.
>    >>> f:int
>    >>> f
>    Traceback (most recent call last):
>      File "<stdin>", line 1, in <module>
>    NameError: name 'f' is not defined
>    >>>
>
> So now `f` has `int` type definitely (for `mypy`'s purposes), and if
> used before assignment raises a distinctive error (versus having the
> value `None`, which you might then pass around, and perhaps successfully
> use in some contexts).
>
> It is probably better on the whole to specify types up front rather than
> relying on `mypy` or similar to infer them. That way (a) you're stating
> your intent and (b) not relying on an inferred type, which if you've got
> bugs may be inferred _wrong_. If `mypy` infers a type incorrectly all
> the subsequent checks will also be flawed, perhaps subtly.
>
Yes.
I also use to make f unavailable (f=None) when something goes wrong and
I don't want to stop the script but of course I could use "del f". I
also need to care about using "try", which might be better than "if" tests.
A thing to think of ...

Thanks.
Paulo

Re: Typing: Is there a "cast operator"?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: drsali...@gmail.com (Dan Stromberg)
Newsgroups: comp.lang.python
Subject: Re: Typing: Is there a "cast operator"?
Date: Sun, 23 Oct 2022 20:14:15 -0700
Lines: 41
Message-ID: <mailman.778.1666581270.20444.python-list@python.org>
References: <tj48k6$ndp$1@gioia.aioe.org>
<CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de 7VGSQUwUGsippaFFhJzcOwN2bWUppEUqJlDG0oiEK7cQ==
Return-Path: <drsalists@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=X8UPtqXF;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'pfxlen:0': 0.03;
'argument': 0.04; '2022': 0.05; 'assumes': 0.07; 'scripts.': 0.07;
'sun,': 0.07; 'cc:addr:python-list': 0.09; 'int': 0.09; 'later,':
0.09; 'cc:no real name:2**0': 0.14; 'assert': 0.16;
'from:addr:drsalists': 0.16; 'from:name:dan stromberg': 0.16;
'incompatible': 0.16; 'none:': 0.16; 'received:209.85.221.54':
0.16; 'received:mail-wr1-f54.google.com': 0.16; 'specify': 0.16;
'statements.': 0.16; 'wrote:': 0.16; 'solve': 0.19;
'cc:addr:python.org': 0.20; "i've": 0.22; 'skip:& 40': 0.22;
'code': 0.23; 'cc:2**0': 0.25; 'this?': 0.29; 'program': 0.31;
'subject:there': 0.32; 'message-id:@mail.gmail.com': 0.32; 'but':
0.32; "i'm": 0.33; 'there': 0.33; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'following': 0.35;
'from:addr:gmail.com': 0.35; 'using': 0.37; 'received:209.85':
0.37; 'way': 0.38; 'received:209': 0.39; 'hello!': 0.39; 'use':
0.39; 'should': 0.40; 'skip:o 10': 0.61; 'skip:\xc2 10': 0.62;
'claims': 0.64; 'look': 0.65; '8bit%:100': 0.76; '(please': 0.84;
'naturally': 0.84; 'you:': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=cc:to:subject:message-id:date:from:in-reply-to:references
:mime-version:from:to:cc:subject:date:message-id:reply-to;
bh=tNFbzD6uw5oI/TS9ZnWrZlnqZE22coXQfn0HB49YTK8=;
b=X8UPtqXFt6FL2FaQyOFRKa9XYXml6JMQsBl/CR2H4xnWN3I13owVsELfy9mznk4E4X
vajpxN1fg4X9k8dmgmW6ca+u1+g3WT0X25kLO/ujMMGuBykTLFr+xEyYj0vQYJ29+9tf
/uO/j0eNFpXWGHg+zr5kNBKEYpMGY3GjgY/LQ55mS2ZYYE83qsnQjHksw2Uid4kCope2
VsG9ESsvX6JM6iUrKIwSn/wMF++8z6P1lLHjbWwY6sVRg9dJpDGCzmMPcD04c+VOtfeq
TqXqWG+eJcyFY8GhNksozOu9xW70X5FfJ/fji1Xz9W7PRYS6aobrj2NWRgCEXQdMkPmk
K7Fg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=cc: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=tNFbzD6uw5oI/TS9ZnWrZlnqZE22coXQfn0HB49YTK8=;
b=POv43/JgM/R9Dxu3TMdFfHpoGZrceDaIArTLQefIXibcQmruBfJf8FF2LsY1GGNJsA
cpsz8wkYv5KpnFyz+8gob6RCbjejFRDKwpd1tWGQBXuKuowOlL3ZTvxf0LVs+ZlP25HQ
mnVF6Ag/8GacI08gCtNwCCYosTHYiPOGsnp0SoxK2wxJjAiVQapp2ezsvlDV+F9P9iv6
NOVJqlH5zZjcVWYnhzYbC80QCRahgQOH64E4cTa7wualXQR1HTC9/LtPtQIHt0+yigAD
7uRaB7y9o4dwQ65QANI7KbF16mKvRr5nLu+I7f/+8d1iIUBs2Ol0MyyFvHYNBftUQwJt
TB+A==
X-Gm-Message-State: ACrzQf1ID728XtEHDmShGXBgBS2/o9biKdLQVZDVTYSnpfU0bLq3cwly
dTRXi64cMxsi5L3ivYEhEQWd7l5h0CtStPycYzkajfdM0xk=
X-Google-Smtp-Source: AMsMyM5NjRXB4yrK18NUY99HTz9z5s/t7HRFxIwXuTHYRBy5h7w0IebGe90ZdBo/9tDM0DG3Yx9BsvL6vIxg8sJxXU4=
X-Received: by 2002:a5d:59af:0:b0:236:6b90:1e65 with SMTP id
p15-20020a5d59af000000b002366b901e65mr3250372wrr.362.1666581267665; Sun, 23
Oct 2022 20:14:27 -0700 (PDT)
In-Reply-To: <tj48k6$ndp$1@gioia.aioe.org>
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
X-Mailman-Original-References: <tj48k6$ndp$1@gioia.aioe.org>
 by: Dan Stromberg - Mon, 24 Oct 2022 03:14 UTC

On Sun, Oct 23, 2022 at 2:11 PM Paulo da Silva <
p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt> wrote:

> Hello!
>
> I am in the process of "typing" of some of my scripts.
> Using it should help a lot to avoid some errors.
> But this is new for me and I'm facing some problems.
>
> Let's I have the following code (please don't look at the program content):
>
> f=None # mypy naturally assumes Optional(int) because later, at open,
> it is assigned an int.
> ..
> if f is None:
> f=os.open(...
> ..
> if f is not None:
> os.write(f, ...)
> ..
> if f is not None:
> os.close(f)
>
> When I use mypy, it claims
> Argument 1 to "write" has incompatible type "Optional[int]"; expected "int"
> Argument 1 to "close" has incompatible type "Optional[int]"; expected "int"
>
> How to solve this?
> Is there a way to specify that when calling os.open f is an int only?
>
> I use None a lot for specify uninitialized vars.
>

I've found that mypy understands simple assert statements.

So if you:
if f is not None:
assert f is not None
os.write(f, ...)

You might be in good shape.

Re: Typing: Is there a "cast operator"?

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

  copy mid

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

  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: Typing: Is there a "cast operator"?
Date: Mon, 24 Oct 2022 14:19:37 +1100
Lines: 19
Message-ID: <mailman.779.1666581591.20444.python-list@python.org>
References: <tj48k6$ndp$1@gioia.aioe.org>
<CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
<CAPTjJmrQusAzr3hZkJPkJfpCuWO3ZPbQ4yr8K-F5FLg+Z9Q-Ww@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de mULrD36ZvcghbAfMFhzbQQxuqckYGMkFhD4yfM0w9+zw==
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=R4kNJkXu;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.044
X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; '2022': 0.05; 'dan': 0.09;
'assert': 0.16; 'bugs': 0.16; 'chrisa': 0.16; 'efficiently,':
0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'none:': 0.16; 'statements.': 0.16; 'wrote:': 0.16; "can't": 0.17;
'bug': 0.19; 'to:addr:python-list': 0.20; "i've": 0.22; 'checks':
0.32; 'subject:there': 0.32; 'message-id:@mail.gmail.com': 0.32;
"i'm": 0.33; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; '"if': 0.35; 'from:addr:gmail.com': 0.35; 'mon,': 0.36;
'received:209.85': 0.37; 'this.': 0.37; 'received:209': 0.39;
'received:209.85.208': 0.39; 'simply': 0.63; 'entire': 0.67;
'deal': 0.73; 'yourself': 0.75; 'happens': 0.84; 'system).': 0.84;
'you:': 0.84; 'fan': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=cs0m1n9wP6yjEailrFBfILkxPGe6X6w7H64DK+X96GI=;
b=R4kNJkXu5e1Q1On+bn/NMrdS/w0R1mB8q+aWpSsqckNfgQ8n2eRiZuPtyM/ov94qhL
oz0xtGb0Fmao+/6YWwmxm4Se85zy0Y7iv/hWDRHpn0SrmiMC4eENUzUD6go2RkUn9XJ7
6gH91fUqeo6PsFmGhezuzPHeG+vOx606kenTgBJVT9M4lwC8oW0TvsGudNPjJslseSP4
4u0Q4xFYLjrqyPBhjLvdbdHIC4sAnVRSjCpYqMrtEVVclayBcKgWygEu/JsS1eGhPJ3q
wRoRk6a8jG4EyUio7u2CpmTpXlTeRuJ7LgExDbY715ktmAr9yPqIZ70qSRCcp+PmUdkq
XeVw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
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=cs0m1n9wP6yjEailrFBfILkxPGe6X6w7H64DK+X96GI=;
b=kZXdb+EMCa3uhUFFwPc07pvVmVcWVk0sNkss/mPTdcYXi+gxKOJEGNyzn9PUEBxRaV
1i/7exrZvOvGmOpnTa3sW5y1f1bGQPVpB79FL0jLELYUCzdRBE+mrvb3+VOiawg2lCN9
+So0Zt+xDglNQJD4XWmoTJUnBbszRvNBB94BdjOaIrokFgR7vXItdlP7jWXSPDm66lLO
C91PORMUT80F8BE48yZhMPqDagBFZ9dRACeOOUtdhB3bgEYxvDCC1ou3BSk+hI/8KVbE
E/MMm99VnHTNUAF1bzV0pNObeMYQhU7h6GHNvCfUte1oQORycG4A2V2ofhudC5O/M+A3
CW4w==
X-Gm-Message-State: ACrzQf1cEy6FLr9S8huWOJUef1+VhnVrOgFNGKAHaAf+9+4+J9rtrTK4
oQ8T6n21KRqQYIUYNfcwOhPvR6Y0Jbdw/Vf4/umozxja
X-Google-Smtp-Source: AMsMyM78OVRionF+pTRG1S6jIDRRGQAU+ZYmTR5ZQwhLMdr9bowCqB7kf+sg5QEQyXYn8CH7XD9DaB5UVz2ruz8RaWQ=
X-Received: by 2002:aa7:d1c3:0:b0:461:c627:2a7b with SMTP id
g3-20020aa7d1c3000000b00461c6272a7bmr2844086edp.231.1666581589128; Sun, 23
Oct 2022 20:19:49 -0700 (PDT)
In-Reply-To: <CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
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: <CAPTjJmrQusAzr3hZkJPkJfpCuWO3ZPbQ4yr8K-F5FLg+Z9Q-Ww@mail.gmail.com>
X-Mailman-Original-References: <tj48k6$ndp$1@gioia.aioe.org>
<CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
 by: Chris Angelico - Mon, 24 Oct 2022 03:19 UTC

On Mon, 24 Oct 2022 at 14:15, Dan Stromberg <drsalists@gmail.com> wrote:
> I've found that mypy understands simple assert statements.
>
> So if you:
> if f is not None:
> assert f is not None
> os.write(f, ...)
>
> You might be in good shape.

Why can't it simply understand the if statement? I'm not a fan of
coddling a type system like this. The entire point of type checking is
to help you find bugs more efficiently, so if you have to repeat
yourself every time you do these kinds of checks just so that mypy is
satisfied, that's counter-productive (case in point: what happens if
you say "if fn is not None: assert f is not None"? Now you've
introduced a bug just to deal with the type system).

ChrisA

Re: Typing: Is there a "cast operator"?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Typing: Is there a "cast operator"?
Date: Mon, 24 Oct 2022 01:24:10 -0400
Lines: 88
Message-ID: <mailman.781.1666593574.20444.python-list@python.org>
References: <tj48k6$ndp$1@gioia.aioe.org>
<CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
<9d3805ae-0acb-65b0-e7d4-3d7bc8a9e170@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de lZMQxfi2fA5zhzbzlNmBoAcCdoFdaemBaHVXd5HIEypQ==
Return-Path: <list1@tompassin.net>
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=tompassin.net header.i=@tompassin.net header.b=hZC9uTE/;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'def':
0.04; '2022': 0.05; 'assumes': 0.07; 'scripts.': 0.07; 'sun,':
0.07; 'dan': 0.09; 'else:': 0.09; 'int': 0.09; 'later,': 0.09;
'prints': 0.09; 'received:23.83.212': 0.09;
'received:elm.relay.mailchannels.net': 0.09; 'import': 0.15;
'42:': 0.16; 'assert': 0.16; 'declare': 0.16; 'incompatible':
0.16; 'none:': 0.16; 'received:10.0.0': 0.16; 'received:64.90':
0.16; 'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'redundant': 0.16; 'specify': 0.16; 'statements.': 0.16; 'typing':
0.16; 'wrote:': 0.16; 'instead': 0.17; 'probably': 0.17; 'solve':
0.19; 'pm,': 0.19; 'to:addr:python-list': 0.20; "i've": 0.22;
'code': 0.23; '(and': 0.25; 'anything': 0.25; 'this?': 0.29;
'header:User-Agent:1': 0.30; 'program': 0.31; 'received:10.0':
0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'subject:there': 0.32;
'but': 0.32; "i'm": 0.33; 'there': 0.33; 'header:In-Reply-To:1':
0.34; 'following': 0.35; 'cases': 0.36; 'using': 0.37; 'way':
0.38; 'two': 0.39; 'hello!': 0.39; 'use': 0.39; 'something': 0.40;
'should': 0.40; 'skip:o 10': 0.61; 'skip:z 10': 0.62; 'claims':
0.64; 'look': 0.65; 'header:Received:6': 0.67; 'received:64':
0.67; 'cast': 0.69; 'skip:f 20': 0.75; '(please': 0.84;
'**please**': 0.84; 'naturally': 0.84; 'none"': 0.84; 'you:': 0.84
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1666589051; a=rsa-sha256;
cv=none;
b=OEv3hLMIsS64NEj6RqSv15C+8sxCeVajpwXsCBIwyWOCksRcrz0ypaItV7f7pZQsPM5RiO
EFV4NKrv3Wr1xtlsfVCaG2lDSOx/xtrNX/WuxroFBN70vym61aWTHZH1GXCrmo9MG03nPv
aLAGJNooOb6klo/Oi5GJq7TU6X+jtg2qLnQiv+371YXjjbjvDWxwS7TcxNJKKO6yYtOE9v
wimQUcbj0aNozNtHvRSFmyj+IzSLa9bV6KrTx/8XROYO9HJBxV5Oaqqqa4pr1Ra34p8nik
eUBn8D5dn52I+EYnPWi6uQVzrXae6BQAh0cwhz4uEQwqZjIHQrNy7jmIHWWCgQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1666589051;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=gXIDq0XyEIUMGD8j7I1ysOHO1fMKv0LXxmUXjmE61rA=;
b=TOhweHiCkUwxONTI4E1pR/33P/P9RP/wzKgSYCWIQDULhZp04GUwPSoPQNsVh/WE76kTKI
JxKc1HcCNjIYH8yE5BJ3bkGlSur5C0wj4Vb5xt7a1QSPrm71oOOZ/KYNDS3rUIyR3upOlF
2OfPjzCMI/A5efpI04HJkoYSUsrm0V0DkMOOI2EFH8PGNq5UG+PgS0hiK2W8826GZcuMTQ
8rMUfXBbaeRQ8Yvl8N2UKxwRkyjUc7+9LRVQQ0XRkY31FP6b1JTcDIM1xoQi/RNzOcE6Be
97TV9w0NWh1Sfsr+6Iz/PYn2n2p3sBZAuwyVL/csNRsTketmKAw2ridrkNaMgg==
ARC-Authentication-Results: i=1; rspamd-6955c7cd5b-9sf55;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Snatch-Lettuce: 66b8e8ed0bbfd78a_1666589051871_3158662745
X-MC-Loop-Signature: 1666589051871:4120413745
X-MC-Ingress-Time: 1666589051871
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1666589051;
bh=gXIDq0XyEIUMGD8j7I1ysOHO1fMKv0LXxmUXjmE61rA=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=hZC9uTE/Im6Kh00fDCeLGF7jcnusI6Bm8PgocvhiJX67T9YS2knGoJLZRoIFU7QFQ
UelbWGK9kpWATeuSW1osUX5yNwqOTt6RYW1IjNrVhqXDbmG2l/KRWgR7J0ZzEGQ616
xrW0aDbx88/ZSV0Jq1kgfj7mPuEbb6vvAAnxe2bfsscsjvOMD4teWa0xonRMqUZMKq
vleLWkXtD5dBWsWjdAWyzsBxMfnxBpdAPLg6wxMg9BJBUcuJt3Dkk6qe/vKByXcvfa
lzMHVVwt6uzWn0Mtc9LIgOLlzTYVcSfK2uSlJ1fT8fNtr8uAiL2p5foBtg6r+5jajy
P887ZYhp19E5g==
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.4.0
Content-Language: en-US
In-Reply-To: <CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
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: <9d3805ae-0acb-65b0-e7d4-3d7bc8a9e170@tompassin.net>
X-Mailman-Original-References: <tj48k6$ndp$1@gioia.aioe.org>
<CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
 by: Thomas Passin - Mon, 24 Oct 2022 05:24 UTC

On 10/23/2022 11:14 PM, Dan Stromberg wrote:
> On Sun, Oct 23, 2022 at 2:11 PM Paulo da Silva <
> p_d_a_s_i_l_v_a_ns@nonetnoaddress.pt> wrote:
>
>> Hello!
>>
>> I am in the process of "typing" of some of my scripts.
>> Using it should help a lot to avoid some errors.
>> But this is new for me and I'm facing some problems.
>>
>> Let's I have the following code (please don't look at the program content):
>>
>> f=None # mypy naturally assumes Optional(int) because later, at open,
>> it is assigned an int.
>> ..
>> if f is None:
>> f=os.open(...
>> ..
>> if f is not None:
>> os.write(f, ...)
>> ..
>> if f is not None:
>> os.close(f)
>>
>> When I use mypy, it claims
>> Argument 1 to "write" has incompatible type "Optional[int]"; expected "int"
>> Argument 1 to "close" has incompatible type "Optional[int]"; expected "int"
>>
>> How to solve this?
>> Is there a way to specify that when calling os.open f is an int only?
>>
>> I use None a lot for specify uninitialized vars.
>>
>
> I've found that mypy understands simple assert statements.
>
> So if you:
> if f is not None:
> assert f is not None
> os.write(f, ...)
>
> You might be in good shape.

I'm not very familiar with anything but the simplest typing cases as
yet, but mypy is happy with these two fragments.

if f:
os.write(int(f)) # f must be an int if it is not None, so we can
cast it to int.

Or something like this (substitute write() for print() as needed) -

from typing import Optional, Any

def f1(x:int)->Optional[int]:
if x == 42:
return x
return None

def zprint(arg:Any):
if type(arg) == int:
print(arg)

y0 = f1(0) # None
y42 = f1(42) # 42

zprint(y0) # Prints nothing
zprint(y42) # Prints 42

Another possibility that mypy is happy with (and probably the simplest)
- just declare g:int = None instead of g = None:

g: int = None
def yprint(arg: int):
if arg:
yprint(arg)
else:
print('arg is None')

yprint(g) # Prints "arg is None"

And **please** let's not go doing this kind of redundant and inelegant
construction:

if f is not None:
assert f is not None
os.write(f, ...)

Re: Typing: Is there a "cast operator"?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: __pete...@web.de (Peter Otten)
Newsgroups: comp.lang.python
Subject: Re: Typing: Is there a "cast operator"?
Date: Wed, 26 Oct 2022 14:44:29 +0200
Lines: 58
Message-ID: <mailman.813.1666788274.20444.python-list@python.org>
References: <tj48k6$ndp$1@gioia.aioe.org>
<CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
<CAPTjJmrQusAzr3hZkJPkJfpCuWO3ZPbQ4yr8K-F5FLg+Z9Q-Ww@mail.gmail.com>
<ec8cdd94-daec-0dfd-7c29-c8aa638ff1d4@web.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 3awRtlvZmsVb8MgQcoD6wwJpY8VY88O4hTofZx/bZ/Ag==
Return-Path: <__peter__@web.de>
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=web.de header.i=@web.de header.b=kdqW1YRh; dkim-adsp=pass;
dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'argument': 0.04; '2022':
0.05; 'error:': 0.05; 'random': 0.05; 'received:212.227': 0.07;
'angelico': 0.09; 'dan': 0.09; 'import': 0.15; 'assert': 0.16;
'bugs': 0.16; 'chrisa': 0.16; 'efficiently,': 0.16;
'from:addr:web.de': 0.16; 'incompatible': 0.16; 'message-
id:@web.de': 0.16; 'none:': 0.16; 'received:mout.web.de': 0.16;
'received:web.de': 0.16; 'statements.': 0.16; 'wrote:': 0.16;
'problem': 0.16; "can't": 0.17; 'bug': 0.19; 'to:addr:python-
list': 0.20; "i've": 0.22; 'received:de': 0.23; 'chris': 0.28;
'error': 0.29; 'header:User-Agent:1': 0.30; 'attempt': 0.31;
'checks': 0.32; 'subject:there': 0.32; "i'm": 0.33; 'header:In-
Reply-To:1': 0.34; '"if': 0.35; 'mon,': 0.36; 'source': 0.36;
'this.': 0.37; 'received:192.168': 0.37; 'file': 0.38; 'could':
0.38; 'skip:o 10': 0.61; 'received:212': 0.62; 'skip:o 20': 0.63;
'simply': 0.63; 'verify': 0.64; 'skip:r 20': 0.64; 'entire': 0.67;
'deal': 0.73; 'yourself': 0.75; 'happens': 0.84; 'success:': 0.84;
'system).': 0.84; 'you:': 0.84; 'fan': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273;
t=1666788272; bh=3Xr9bvlsjfNsGK47aigTPqUP0UPf66LCHuZZAG+fOZY=;
h=X-UI-Sender-Class:Date:Subject:To:References:From:In-Reply-To;
b=kdqW1YRhUAfgj7pujYb1gkMe0sjrAPrI7pAVtU1t1vGce1gPAz9m7pyqBDbtdgO49
Wo4yw8B5SYHFel8+ukaw8CnEq5Ex9Yap/TW3I3sYTF4LG943WhdMTtbp0m5n9kJZ02
i/8Jud5mwEgu5T7temwzUyKvp1S4+8uOPFb49PMFteUQEhiaCgBgYdQNEUFnWicDxg
g/DT8bSETuHGpeFnPYL/hsQpaRF/M1KyFe7MXHngRlsTYcTJm3D51g/LaIelbWZvGK
ow60VC7RO2dk+9JQpKAEVoq3Xa2zbWbBhBLf7kb07mxCNUo3G7e7jrkN0PxBrHXyeO
nO7bYy4mLdKeA==
X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101
Thunderbird/102.4.0
Content-Language: en-US
In-Reply-To: <CAPTjJmrQusAzr3hZkJPkJfpCuWO3ZPbQ4yr8K-F5FLg+Z9Q-Ww@mail.gmail.com>
X-Provags-ID: V03:K1:Up+VNYfLTpHJbt8qaX4L+Fxfm0GJC+Tv+4AP3HvP/UdfinQ6fik
Kx2nGljO7XYnCIcCgDE8x1R+KHb5bnOsSU99Kb6PDnp1KXQYYW1rEGMQd/Y+9bVCvAXuIGp
+8RQcR2ivHso81mhd7vGZ5Ch/iDtRqVSt3dYAMqNRghj3/vvaGoJVFXxqm1n94BQ6VMIyLC
p0tE3jEcNueBt+PanpS0Q==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:IcBcnnLAS0I=;Q8o+DESE6fW4GqdSshTpQwSYzak
NpGN+vIMPxSrLTY4sVpUcinBMtD8fC+uNW0YVbtzpUjVqFmfvQM9V/Qb1ODowHBiGuHtmbYEC
9tNo8XIyrGfkZxTVfeyl1mWFEoV9+0ihD0hwFkpJq+SIs1PPZzxxUF1O2AgiGorlUqRQWrN7J
dKHwIp30l/tSRH0IcV74z76vCx8GE6oA9coD+9pCnM6MRmiTAZD3Eli0reJZjrgcPcmFzZ3IU
sk0RkaT5HK+S4fWGJSaOnBeanaWCaAJlev93KlzYytaImJLM4TvILCgN9QsJAzruVfqgUI3Ga
ivsvMhdYu0RQ1VMWr/Yu481uISE1Er0p+teBANfm522F8zC0x1+aiFcD8jMQT4lQP8oHcEFgl
m0xPOpxwSnfZCUM8mR2T9KEwlwa2Jq+T4vkN/mzlH972Pxd5fE579TWOOT5mh9xtsXqOSrypF
MkYTNwRATRZiilZUvakclFWwDNQ9QlRC20CInkGUgWiotqf7tGtTtfesyoFB7bOepQZPd3dOW
DB2NTHgX6PROT6LeiZ+HmbSlIE1lwMqQtGAIEtdT32vTuBM/He4/sN1N7MHYuZAmY5ADmz7nz
JI60HmcctSOKUcNNuvDwYxS3KWi7BUsvueUaQYnWFMTTvMMGIsxVkvB1wOHe/cM9PJY+p5ccQ
xvcAsUhD1ltNEcWk/YPNHjnTU3qmwx+bnHQS5+szrWAN3y6hjk3FIFPKnTlGGGf+lDfpm9E7u
8r/G1kLWmpncWTNSiGOiPXb7nBM8G8AAQ/HP5M/JIf23ah0vX6H1guQhN//GjyfmjWxh5Odu+
RHoZNHjxp20lADgUDdLtgd16J3zuJUza4z7jnVj1aSOoJCdfVD+1iknSvh6fDV7rJSABwqAjk
p9KZnVvI2q3A2J6P7QBpWdWJFuL7SK90tQlSEbI923df2SudDzF5UmoET9YSUcwyGOXFWMXsL
pfDy0A==
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: <ec8cdd94-daec-0dfd-7c29-c8aa638ff1d4@web.de>
X-Mailman-Original-References: <tj48k6$ndp$1@gioia.aioe.org>
<CAGGBd_o=yYXqoef_JoAEFKsdEO_ER=UTYtsTdbh9TRf6irrP7g@mail.gmail.com>
<CAPTjJmrQusAzr3hZkJPkJfpCuWO3ZPbQ4yr8K-F5FLg+Z9Q-Ww@mail.gmail.com>
 by: Peter Otten - Wed, 26 Oct 2022 12:44 UTC

On 24/10/2022 05:19, Chris Angelico wrote:
> On Mon, 24 Oct 2022 at 14:15, Dan Stromberg <drsalists@gmail.com> wrote:
>> I've found that mypy understands simple assert statements.
>>
>> So if you:
>> if f is not None:
>> assert f is not None
>> os.write(f, ...)
>>
>> You might be in good shape.
>
> Why can't it simply understand the if statement?

Could it be that this specific problem is fixed in current mypy?
I get

$ type .\tmp.py
import os

f = None
if f is None:
f = os.open("tmp.txt", os.O_RDWR|os.O_CREAT)
os.write(f, b"yadda")
$ mypy tmp.py
Success: no issues found in 1 source file

My attempt to verify that

if name is None: ...

is recognized:

$ type .\tmp2.py
import os
import random

f = None
if random.randrange(2):
f = os.open("tmp.txt", os.O_RDWR|os.O_CREAT)
os.write(f, b"yadda")
$ mypy tmp2.py
tmp2.py:7: error: Argument 1 to "write" has incompatible type
"Optional[int]"; expected "int"
Found 1 error in 1 file (checked 1 source file)
$ mypy --version
mypy 0.982 (compiled: yes)
$

I'm not a fan of
> coddling a type system like this. The entire point of type checking is
> to help you find bugs more efficiently, so if you have to repeat
> yourself every time you do these kinds of checks just so that mypy is
> satisfied, that's counter-productive (case in point: what happens if
> you say "if fn is not None: assert f is not None"? Now you've
> introduced a bug just to deal with the type system).
>
> ChrisA

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor