You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ mypy --pretty --show-error-codes --check-untyped-defs example.py
example.py:6: error: Unsupported operand types for & ("List[bytes]" and "int") [operator]
new[3] = new[3] & ~termios.ECHO # lflags
^
example.py:6: note: Left operand is of type "Union[int, List[bytes]]"
Found 1 error in 1 file (checked 1 source file)
Expected Behavior
The code typechecks successfully, because it is correct. In the reality of runtime, I never have to worry that new[3] will be a list, because it won't.
The mypy error is caused by the definition of _Attr, which is the return type of tcgetattr:
Typeshed's own contribution guidelines, python/mypy#1693, and mypy's docs agree that union return types are bad news. I can get around this with an assert isinstance(new[3], int), but at runtime new[0] through new[5] will always be int, and new[6] will always be List[bytes] *, so the assertion is pointless.
It would be clearer if tcgetattr returned an object so the different values could have their own names and types, but that decision predates typeshed. It would be nice if we at least had an AnyOf type (python/typing#566) so we could write List[AnyOf[int, List[AnyOf[bytes, int]]]], but we don't. So we are left with List[Any], which is not very specific but is at least not incorrect.
* Except for new[6][termios.VTIME] and new[6][termios.VMIN], which are int in noncanonical mode (when new[3] & termios.ICANON is zero). The stubs currently ignore this possibility.
The text was updated successfully, but these errors were encountered:
Returning a union from tcgetattr forces the caller to use isinstance or
a type: ignore comment if they modify the returned list. Return
List[Any] instead.
The cc field has ints at cc[termios.VTIME] and cc[termios.VMIN] if
lflag & termios.ICANON is zero. Change _Attr to allow this.
Fixespython#4661
Returning a union from tcgetattr forces the caller to use isinstance or
a type: ignore comment if they modify the returned list. Return
List[Any] instead.
The cc field has ints at cc[termios.VTIME] and cc[termios.VMIN] if
lflag & termios.ICANON is zero. Change _Attr to allow this.
Fixes#4661
Example Code
The example from the termios docs:
Actual Behavior
Expected Behavior
The code typechecks successfully, because it is correct. In the reality of runtime, I never have to worry that
new[3]
will be a list, because it won't.The mypy error is caused by the definition of _Attr, which is the return type of tcgetattr:
typeshed/stdlib/2and3/termios.pyi
Line 4 in 0142a87
Typeshed's own contribution guidelines, python/mypy#1693, and mypy's docs agree that union return types are bad news. I can get around this with an
assert isinstance(new[3], int)
, but at runtimenew[0]
throughnew[5]
will always beint
, andnew[6]
will always beList[bytes]
*, so the assertion is pointless.It would be clearer if tcgetattr returned an object so the different values could have their own names and types, but that decision predates typeshed. It would be nice if we at least had an AnyOf type (python/typing#566) so we could write
List[AnyOf[int, List[AnyOf[bytes, int]]]]
, but we don't. So we are left withList[Any]
, which is not very specific but is at least not incorrect.* Except for
new[6][termios.VTIME]
andnew[6][termios.VMIN]
, which are int in noncanonical mode (whennew[3] & termios.ICANON
is zero). The stubs currently ignore this possibility.The text was updated successfully, but these errors were encountered: