Check the target of NewType [valid-newtype]
The target of a NewType
definition must be a class type. It can’tbe a union type, Any
, or various other special types.
You can also get this error if the target has been imported from amodule whose source mypy cannot find, since any such definitions aretreated by mypy as values with Any
types. Example:
- from typing import NewType
- # The source for "acme" is not available for mypy
- from acme import Entity # type: ignore
- # Error: Argument 2 to NewType(...) must be subclassable (got "Any") [valid-newtype]
- UserEntity = NewType('UserEntity', Entity)
To work around the issue, you can either give mypy access to the sourcesfor acme
or create a stub file for the module. See Missing importsfor more information.