defaults
would lose its typing when used in inherited class? This only applies to an attribute with the name defaults
.
=None
and the stubs have : trio.abc.Clock = ...
. seems like it would be good to have some tool or another able to find this discrepancy.
mypy.nodes.TypeInfo
:]
) 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?