pytype has the following classes of errors, which can be disabled with a
pytype: disable=error-class
directive. For example, to suppress an
error for a missing attribute foo
:
x = a.foo # pytype: disable=attribute-error
or, to suppress all attribute errors for a block of code:
# pytype: disable=attribute-error
x = a.foo
y = a.bar
# pytype: enable=attribute-error
See Silencing Errors for a more detailed example.
- Error classes
- annotation-type-mismatch
- attribute-error
- bad-concrete-type
- bad-function-defaults
- bad-return-type
- bad-slots
- bad-unpacking
- base-class-error
- container-type-mismatch
- duplicate-keyword-argument
- ignored-abstractmethod
- ignored-metaclass
- ignored-type-comment
- import-error
- invalid-annotation
- invalid-directive
- invalid-function-definition
- invalid-function-type-comment
- invalid-namedtuple-arg
- invalid-super-call
- invalid-typevar
- key-error
- late-directive
- missing-parameter
- module-attr
- mro-error
- name-error
- not-callable
- not-indexable
- not-instantiable
- not-supported-yet
- not-writable
- pyi-error
- python-compiler-error
- recursion-error
- redundant-function-type-comment
- reveal-type
- unbound-type-param
- unsupported-operands
- wrong-arg-count
- wrong-arg-types
- wrong-keyword-args
A variable had a type annotation and an assignment with incompatible types.
Example:
x: int = 'hello world'
NOTE: This is currently only checked for fields within a @dataclass definition.
The attribute being accessed may not exist. Often, the reason is that the
attribute is declared in a method other than __new__
or __init__
:
class A(object):
def make_foo(self):
self.foo = 42
def consume_foo(self):
return self.foo # attribute-error
To make pytype aware of foo
, declare its type with a variable annotation:
class A(object):
foo: int
NOTE: This declaration does not define the attribute at runtime.
The above variable annotation syntax is available only in Python 3.6+, so in earlier versions, declare a class attribute (with the literal ellipses) and supply the type in a type comment:
class A(object):
foo = ... # type: int
NOTE: This pattern does define a runtime attribute.
A generic type was instantiated with incorrect concrete types. Example:
from typing import Generic, TypeVar
T = TypeVar('T', int, float)
class A(Generic[T]):
pass
obj = A[str]() # bad-concrete-type
An attempt was made to set the __defaults__
attribute of a function with an
object that is not a constant tuple. Example:
import collections
X = collections.namedtuple("X", "a")
X.__new__.__defaults__ = [None] # bad-function-defaults
At least one of the possible types for the return value does not match the declared return type. Example:
def f(x) -> int:
if x:
return 42
else:
return None # bad-return-type
NOTE: For the corner case of an empty function whose body is a docstring, use
the block form of disable
to suppress the error:
# pytype: disable=bad-return-type
def f() -> int:
"""Override in subclasses and return int."""
# pytype: enable=bad-return-type
An attempt was made to set the __slots__
attribute of a class using an object
that's not a string.
class Foo(object):
__slots__ = (1, 2, 3)
A tuple was unpacked into the wrong number of variables. Example:
a, b = (1, 2, 3) # bad-unpacking
The class definition uses an illegal value for a base class. Example:
class A(42): # base-class-error
pass
A method call violated the type annotation of a container by modifying its contained type.
Example:
a: List[int] = [1, 2]
a.append("hello") # <-- contained type is now Union[int, str]
NOTE: This is currently only checked when using the --check-variable-types
flag.
A positional argument was supplied again as a keyword argument. Example:
def f(x):
pass
f(True, x=False) # duplicate-keyword-argument
If you believe you are seeing this error due to a bug on pytype's end, see this section for where the type information we use is located.
The abc.abstractmethod decorator was used in a non-abstract class. Example:
import abc
class A(object): # ignored-abstractmethod
@abc.abstractmethod
def f(self):
pass
Add the abc.ABCMeta
metaclass to fix this issue:
import abc
class A(metaclass=abc.ABCMeta):
@abc.abstractmethod
def f(self):
pass
A Python 2-only metaclass declaration was found. Example:
class A(object):
__metaclass__ = Meta
For Python 3-only code, the fix is:
class A(metaclass=Meta):
...
For Python 2-and-3 code, two equally good fixes are:
import six
@six.add_metaclass(Meta)
class A(object):
...
or:
import six
class A(six.with_metaclass(Meta, object)):
...
A type comment was found on a line on which type comments are not allowed. Example:
def f():
return 42 # type: float # ignored-type-comment
The module being imported was not found.
Something is wrong with this annotation. Examples:
from typing import List, TypeVar, Union
T = TypeVar("T")
condition = ... # type: bool
class _Foo: ...
def Foo():
return _Foo()
def f(x: List[int, str]): # bad: too many parameters for List
pass
def f(x: T): # bad: the TypeVar appears only once in the signature
pass
def f(x: Foo): # bad: not a type
pass
def f(x: Union): # bad: no options in the union
pass
def f(x: int if condition else str): # bad: ambiguous type
pass
You will also see this error if you use a forward reference in typing.cast or
pass a bad type to attr.ib
:
import attr
import typing
v = typing.cast("A", None) # invalid-annotation
class A(object):
pass
@attr.s
class Foo(object):
v = attr.ib(type=zip) # invalid-annotation
The solutions are to use a type comment and to fix the type:
import attr
v = None # type: "A"
class A(object):
pass
@attr.s
class Foo(object):
v = attr.ib(type=list)
The error name is misspelled in a pytype disable/enable directive. Example with
a misspelled name-error
:
x = TypeDefinedAtRuntime # pytype: disable=nmae-error # invalid-directive
An invalid function was constructed via metaprogramming (typically with a
decorator such as @dataclass
). Example:
from dataclasses import dataclass
@dataclass
class A:
x: int = 10
y: str
which creates
def __init__(x: int = 10, y: str):
...
with a non-default argument following a default one.
Something was wrong with this function type comment. Examples:
def f(x):
# type: (int) # bad: missing return type
pass
def f(x):
# type: () -> None # bad: too few arguments
pass
def f(x):
# type: int -> None # bad: missing parentheses
pass
The typename or one of the field names in the namedtuple definition is invalid. Field names:
- must not be a Python keyword,
- must consist of only alphanumeric characters and "_",
- must not start with "_" or a digit.
Also, there can be no duplicate field names. The typename has the same requirements, except that it can start with "_".
A call to super without any arguments (Python 3) is being made from an invalid context. A super call without any arguments should be made from a method or a function defined within a class. Also, the caller should have at least one positional argument.
Something was wrong with this TypeVar definition. Examples:
from typing import TypeVar
T = TypeVar("S") # bad: storing TypeVar "S" as "T"
T = TypeVar(42) # bad: using a non-str value for the TypeVar name
T = TypeVar("T", str) # bad: supplying a single constraint (did you mean `bound=str`?)
T = TypeVar("T", 0, 100) # bad: 0 and 100 are not types
The dictionary key doesn't exist. Example:
x = {}
y = x["y"] # key-error
A # pytype: disable
without a matching following enable or a # type: ignore
appeared on its own line after the first top-level definition. Such a directive
takes effect for the rest of the file, regardless of indentation, which is
probably not what you want:
def f() -> bool:
# pytype: disable=bad-return-type # late-directive
return 42
Two equally acceptable fixes:
def f() -> bool:
return 42 # pytype: disable=bad-return-type
# pytype: disable=bad-return-type
def f() -> bool:
return 42
# pytype: enable=bad-return-type
The function was called with a parameter missing. Example:
def add(x, y):
return x + y
add(42) # missing-parameter
If you believe you are seeing this error due to a bug on pytype's end, see this section for where the type information we use is located.
The module attribute being accessed may not exist. Example:
import sys
sys.nonexistent_attribute # module-attr
A valid method resolution order cannot be created for the class being defined. Often, the culprit is cyclic inheritance:
class A(object):
pass
class B(object, A): # mro-error
pass
This name does not exist in the current namespace. Note that types like List
,
Dict
, etc., need to be imported from the typing module:
MyListType = List[str] # name-error
from typing import List
MyListType = List[str]
The object being called or instantiated is not callable. Example:
x = 42
y = x() # not-callable
The object being indexed is not indexable. Example:
tuple[3] # not-indexable
The class cannot be instantiated because it has abstract methods. Example:
import abc
class A(metaclass=abc.ABCMeta):
@abc.abstractmethod
def f(self):
pass
A() # not-instantiable
This feature is not yet supported by pytype.
The object an attribute was set on doesn't have that attribute, or that attribute isn't writable:
class Foo(object):
__slots__ = ("x", "y")
Foo().z = 42 # not-writable
The pyi file contains a syntax error.
If you encounter this error in a pyi file that you did not create yourself, please file a bug.
The Python code contains a syntax error.
A recursive definition was found in a pyi file. Example:
class A(B): ...
class B(A): ...
If you encounter this error in a pyi file that you did not create yourself, please file a bug.
Using both inline annotations and a type comment to annotate the same function is not allowed. Example:
def f() -> None:
# type: () -> None # redundant-function-type-comment
pass
The error message displays the type of the expression passed to it. Example:
import os
reveal_type(os.path.join("hello", u"world")) # reveal-type: unicode
This feature is implemented as an error to ensure that reveal_type()
calls are
removed after debugging.
This error currently applies only to pyi files. The type parameter is not bound to a class or function. Example:
from typing import AnyStr
x = ... # type: AnyStr # unbound-type-param
Unbound type parameters are meaningless as types. If you want to take advantage of types specified by a type parameter's constraints or bound, specify those directly. So the above example should be rewritten as:
from typing import Union
x = ... # type: Union[str, unicode]
A binary operator was called with incompatible arguments. Example:
x = "hello" ^ "world" # unsupported-operands
The function was called with the wrong number of arguments. Example:
def add(x, y):
return x + y
add(1, 2, 3) # wrong-arg-count
If you believe you are seeing this error due to a bug on pytype's end, see this section for where the type information we use is located.
The function was called with the wrong argument types. Example:
def f(x: int):
pass
f(42.0) # wrong-arg-types
If you believe you are seeing this error due to a bug on pytype's end, see this section for where the type information we use is located.
The function was called with the wrong keyword arguments. Example:
def f(x=True):
pass
f(y=False) # wrong-keyword-args
If you believe you are seeing this error due to a bug on pytype's end, see this section for where the type information we use is located.