À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



--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to