Hello!And yes there is! Exactly the "cast" operator. A mistype led me to wrong
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 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.
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.
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.
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.
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?
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
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 79:25:46 |
Calls: | 6,716 |
Files: | 12,247 |
Messages: | 5,357,912 |