termios: Definition of tcgetattr causes false positives #4661
Comments
Thanks for the report! I'd merge a PR changing this to |
Cebtenzzre
added a commit
to Cebtenzzre/typeshed
that referenced
this issue
Oct 12, 2020
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 python#4661
hauntsaninja
pushed a commit
that referenced
this issue
Oct 13, 2020
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: