:]
) stubtest.
error: package.f is inconsistent, runtime argument "p" has a default value of type None, which is incompatible with stub argument type builtins.int
Stub: at line 1
def (p: builtins.int =)
Runtime: at line 1 in file /Users/shantanu/tmp/lol/package/__init__.py
def (p=None)
from typing import Union
def foo(X: Union[int, str]) -> bool:
if isinstance(X, str):
reveal_type(X) # str
def g(y: int) -> bool:
reveal_type(X) # Union[int, str]
return len(X) > y # <<<<< error: Argument 1 to "len" has incompatible type "Union[int, str]"; expected "Sized"
return g(10)
reveal_type(X) # int
return bool(X)
Any
. I'm currently running into a snag where I don't know how to deal with a NewType
that I defined - what I'm trying to do results in a mypy error. Please find below a minimal reproduction of the problem. Does anyone have any advice or am I forced to leave it as Any
?# test.py
from dataclasses import dataclass
from typing import NewType
A = NewType("A", str)
@dataclass
class Test:
a: A
# stubgen'd test.pyi
from typing import Any
A: Any
class Test:
a: A
def __init__(self, a: Any) -> None: ...
# trying to remove 'Any' as much as possible
from typing import Type
A: Type[str]
class Test:
a: A # mypy error: "test.A" is not valid as a type
def __init__(self, a: A) -> None: ... # mypy error: "test.A" is not valid as a type
% dmypy check -- --local-partial-types dtests/dtests.py dtests/tests
Success: no issues found in 430 source files
% mypy --local-partial-types dtests/dtests.py dtests/tests
dtests/dtestlib/scales/AbstractIterableComparator.py:36: error: Cannot determine type of '_result'
dtests/dtestlib/scales/AbstractIterableComparator.py:39: error: Need type annotation for '_diff' (hint: "_diff: List[<type>] = ...")
dtests/dtestlib/scales/AbstractIterableComparator.py:72: error: Incompatible types in assignment (expression has type "None", variable has type "List[Any]")
dtests/dtestlib/scales/TypeStrictComparator.py:28: error: Cannot determine type of '_result'
dtests/dtestlib/scales/TypeStrictComparator.py:29: error: Cannot determine type of '_result'
dtests/dtestlib/scales/TypeStrictComparator.py:29: error: unused 'type: ignore' comment
dtests/dtestlib/scales/DictComparator.py:34: error: Incompatible types in assignment (expression has type "Dict[<nothing>, <nothing>]", variable has type "List[Any]")
dtests/dtestlib/scales/DictComparator.py:53: error: Incompatible types in assignment (expression has type "None", variable has type "List[Any]")
dtests/dtestlib/scales/DictComparator.py:33: error: unused 'type: ignore' comment
dtests/dtestlib/scales/DictComparator.py:49: error: unused 'type: ignore' comment
dtests/dtestlib/scales/RelaxedComparator.py:28: error: Cannot determine type of '_result'
dtests/dtestlib/scales/RelaxedComparator.py:29: error: Cannot determine type of '_result'
dtests/dtestlib/scales/RelaxedComparator.py:35: error: Redundant cast to "bool"
dtests/dtestlib/scales/RelaxedComparator.py:29: error: unused 'type: ignore' comment
Hi, I'm running into an issue with typevars unifying into object
and therefore allowing incorrect code. Example:
T = TypeVar("T")
class Field(Generic[T]):
dtype: Type[T]
default: T
foo = Field(int, "asdf") # I expect this to fail, but instead it typechecks since foo is a Field[object]
I found this issue which seems similar but is closed: python/mypy#6559
Is what I'm looking for possible in mypy?
Optional
when the expression type is None
? I would be happy to write a PR.
nan
as a replacement for None, as it is possible to accidentally produce a nan
value.inf
or nan
, but it's still possible to get nan
for example by calculating inf - inf
I'm not sure -- it worked for me?
shell:
PS C:\Users\A5rocks\Documents\boto-test> python -m venv venv
...
PS C:\Users\A5rocks\Documents\boto-test> .\venv\Scripts\activate
...
(venv) PS C:\Users\A5rocks\Documents\boto-test> pip install mypy boto3-stubs boto3
...
<open editor and put my code for example.py into, well, example.py>
(venv) PS C:\Users\A5rocks\Documents\boto-test> mypy example.py
example.py:3: note: Revealed type is 'def (self: boto3.NullHandler, record: Any) -> Any'
example.py
:
import boto3
reveal_type(boto3.NullHandler.emit)
(not sure if the Any
s were what you were talking about...)
I have encountered a strange behavior with any()
. This is a simplified example:
import typing
T = typing.TypeVar('T')
def children(_: T) -> typing.Iterable[T]: return []
def orig(node: str) -> typing.Optional[str]:
return any(orig(child) for child in children(node))
def no_errors(node: int) -> typing.Optional[int]:
return any(no_errors(child) for child in children(node))
this is the output of mypy:
$ mypy mypyany.py
mypyany.py:6: error: Incompatible return value type (got "bool", expected "Optional[str]")
Found 1 error in 1 file (checked 1 source file)
$
I don't get the error in the orig()
function (I can "suppose" is because any()
is "too dynamic" returning the first element of a sequence of anything). But, I don't get why, just changing the return annotation type (as in no_errors
) the error goes away!
def my_decorator(f: Callable[..., T]) -> Callable[..., Container[T]]:...