Skip to content

Plugin to typecheck attrs-generated classes #4397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 84 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
005547a
WIP: attrs_plugin_real
euresti Jan 11, 2018
ecd0164
Handle untyped attrs
euresti Jan 11, 2018
8f4dbd7
Handle auto_attribs and refactor
euresti Jan 11, 2018
2b55334
WIP: attrs_plugin
euresti Jan 11, 2018
83b008c
WIP: attrs_plugin
euresti Jan 11, 2018
3bc5c11
WIP: attrs_plugin
euresti Jan 11, 2018
aa55fe5
WIP: attrs_plugin
euresti Jan 11, 2018
5f897b7
WIP: attrs_plugin
euresti Jan 11, 2018
8867278
Support cmp
euresti Jan 11, 2018
cd2aded
WIP: attrs_plugin
euresti Jan 11, 2018
104f9f4
WIP: attrs_plugin
euresti Jan 11, 2018
6856500
WIP: attrs_plugin
euresti Jan 11, 2018
0e359a8
WIP: attr_pyi
euresti Jan 11, 2018
ebb9834
WIP: attr_pyi
euresti Jan 11, 2018
001a16a
WIP: attr_pyi
euresti Jan 11, 2018
453c8ae
WIP: attrs_plugin
euresti Jan 11, 2018
d26bdb6
WIP: attrs_plugin
euresti Jan 11, 2018
e78c040
WIP: attr_pyi
euresti Jan 11, 2018
fd1a24b
WIP: attr_pyi
euresti Jan 11, 2018
1680e8f
Support inheritance
euresti Jan 11, 2018
521d215
Fix lint and mypy
euresti Jan 11, 2018
071c0fd
Optionals
euresti Jan 11, 2018
6986547
Never forget Python 3.5
euresti Jan 11, 2018
5d60d99
Sigh
euresti Jan 11, 2018
2284797
Support nested classes
euresti Jan 11, 2018
89f3537
WIP: attrs_plugin
euresti Jan 11, 2018
353319c
Simplify stubs
euresti Jan 11, 2018
72cc0a7
Cleanup tests
euresti Jan 11, 2018
e48091f
Get rid of self type.
euresti Jan 11, 2018
2d0f5dd
Comments
euresti Jan 11, 2018
f7f6033
Remove magic numbers
euresti Jan 11, 2018
2ff3a19
Cleanup argument pulling
euresti Jan 11, 2018
d06bd74
Fix multiple inheritance override
euresti Jan 11, 2018
6fbe81f
Support type=
euresti Jan 11, 2018
ccfd6c0
Support frozen=True
euresti Jan 11, 2018
4d9c182
Support @attr.dataclass
euresti Jan 11, 2018
2fc6f5f
Add more tests
euresti Jan 11, 2018
d11c138
Cleanup
euresti Jan 11, 2018
dd947d2
Support x.default decorator
euresti Jan 11, 2018
ce2cac6
Fix issue with classmethods calling cls
euresti Jan 11, 2018
d05c364
Get rid of accept. It was causing weird issues
euresti Jan 11, 2018
695b422
Fix Forward Reference Resolution issue
euresti Jan 11, 2018
76fa908
Better types for argument lookups
euresti Jan 11, 2018
405204d
Use type= to set the annotation
euresti Jan 11, 2018
536117f
Add Python2 annotation test
euresti Jan 11, 2018
0fa72a9
Support convert= (Happy New Year!)
euresti Jan 11, 2018
6506d7f
Make cmp methods work with superclasses
euresti Jan 11, 2018
5d16792
Do the correct MRO handling
euresti Jan 11, 2018
9fb4e30
Don't traverse superclasses a 2nd time
euresti Jan 11, 2018
256b446
Support converter
euresti Jan 11, 2018
d566806
Remove LastUpdatedOrderedDict
euresti Jan 11, 2018
5b7b290
Move Attribute list into plugin, to avoid touching TypeInfo
euresti Jan 11, 2018
b2f075e
Get the argument ordering from the stub instead of hardcoded
euresti Jan 11, 2018
f6f2f41
De-flake
euresti Jan 11, 2018
6f60854
Some CR
euresti Jan 17, 2018
7492278
Get rid of called_function helper
euresti Jan 18, 2018
28c7b8d
Add some more tests
euresti Jan 18, 2018
aeb01be
Small cleanups
euresti Jan 19, 2018
4fcb5dd
abstract
euresti Jan 19, 2018
1ddb89e
Add more tests.
euresti Jan 19, 2018
5155480
Split get_bool_argument
euresti Jan 19, 2018
ff00d73
Fix auto_attribs=True
euresti Jan 20, 2018
bc9db4b
Add some python 2 checks
euresti Jan 20, 2018
a4f34a8
Warn against sharing one attrib
euresti Jan 21, 2018
d7a8e2e
Move helper methods out
euresti Jan 22, 2018
6b86aa2
Merge branch 'master' into attrs_plugin
euresti Jan 22, 2018
8eae273
Fix for new messages
euresti Jan 22, 2018
93f80d6
Cleanup imports
euresti Jan 22, 2018
8cdcbfa
De-flake
euresti Jan 22, 2018
70e25dd
Cleanup comment
euresti Jan 22, 2018
fc2b22b
Add warning for using convert
euresti Jan 24, 2018
95e3ac0
Fix bug found when using real stubs
euresti Jan 30, 2018
a076957
Move all attrs code to attrs_plugin.py
euresti Feb 2, 2018
3fa9e4f
Refactor code, split into helper methods
euresti Feb 3, 2018
cad44bb
Work around circular import
euresti Feb 4, 2018
0240092
Support incremental mode with attrs
euresti Feb 5, 2018
c37147d
A little more refactoring
euresti Feb 5, 2018
cb795cd
Handle converter in one place
euresti Feb 5, 2018
b2b538c
Merge branch 'master' into attrs_plugin
euresti Feb 5, 2018
d1e3e0b
Sync typeshed
euresti Feb 8, 2018
6ef4a63
Merge branch 'master' into attrs_plugin
euresti Feb 10, 2018
feab654
Fix test
euresti Feb 10, 2018
641230d
Move attrs_plugin to plugins/attrs
euresti Feb 11, 2018
bf49a87
Add more incremental tests
euresti Feb 13, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,10 @@ class is generic then it will be a type constructor of higher kind.
# needed during the semantic passes.)
replaced = None # type: TypeInfo

# This is a dictionary that will be serialized and un-serialized as is.
# It is useful for plugins to add their data to save in the cache.
metadata = None # type: Dict[str, JsonDict]

FLAGS = [
'is_abstract', 'is_enum', 'fallback_to_any', 'is_named_tuple',
'is_newtype', 'is_protocol', 'runtime_protocol'
Expand All @@ -2016,6 +2020,7 @@ def __init__(self, names: 'SymbolTable', defn: ClassDef, module_name: str) -> No
self._cache = set()
self._cache_proper = set()
self.add_type_vars()
self.metadata = {}

def add_type_vars(self) -> None:
if self.defn.type_vars:
Expand Down Expand Up @@ -2218,6 +2223,7 @@ def serialize(self) -> JsonDict:
'typeddict_type':
None if self.typeddict_type is None else self.typeddict_type.serialize(),
'flags': get_flags(self, TypeInfo.FLAGS),
'metadata': self.metadata,
}
return data

Expand All @@ -2244,6 +2250,7 @@ def deserialize(cls, data: JsonDict) -> 'TypeInfo':
else mypy.types.TupleType.deserialize(data['tuple_type']))
ti.typeddict_type = (None if data['typeddict_type'] is None
else mypy.types.TypedDictType.deserialize(data['typeddict_type']))
ti.metadata = data['metadata']
set_flags(ti, data['flags'])
return ti

Expand Down Expand Up @@ -2612,3 +2619,10 @@ def check_arg_names(names: Sequence[Optional[str]], nodes: List[T], fail: Callab
fail("Duplicate argument '{}' in {}".format(name, description), node)
break
seen_names.add(name)


def is_class_var(expr: NameExpr) -> bool:
"""Return whether the expression is ClassVar[...]"""
if isinstance(expr.node, Var):
return expr.node.is_classvar
return False
41 changes: 38 additions & 3 deletions mypy/plugin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
"""Plugin system for extending mypy."""

from collections import OrderedDict
from abc import abstractmethod
from functools import partial
from typing import Callable, List, Tuple, Optional, NamedTuple, TypeVar

from mypy.nodes import Expression, StrExpr, IntExpr, UnaryExpr, Context, DictExpr, ClassDef
import mypy.plugins.attrs
from mypy.nodes import (
Expression, StrExpr, IntExpr, UnaryExpr, Context, DictExpr, ClassDef,
TypeInfo, SymbolTableNode
)
from mypy.tvar_scope import TypeVarScope
from mypy.types import (
Type, Instance, CallableType, TypedDictType, UnionType, NoneTyp, FunctionLike, TypeVarType,
Type, Instance, CallableType, TypedDictType, UnionType, NoneTyp, TypeVarType,
AnyType, TypeList, UnboundType, TypeOfAny
)
from mypy.messages import MessageBuilder
Expand Down Expand Up @@ -56,6 +61,9 @@ def named_generic_type(self, name: str, args: List[Type]) -> Instance:
class SemanticAnalyzerPluginInterface:
"""Interface for accessing semantic analyzer functionality in plugins."""

options = None # type: Options
msg = None # type: MessageBuilder

@abstractmethod
def named_type(self, qualified_name: str, args: Optional[List[Type]] = None) -> Instance:
raise NotImplementedError
Expand All @@ -69,6 +77,22 @@ def fail(self, msg: str, ctx: Context, serious: bool = False, *,
blocker: bool = False) -> None:
raise NotImplementedError

@abstractmethod
def anal_type(self, t: Type, *,
tvar_scope: Optional[TypeVarScope] = None,
allow_tuple_literal: bool = False,
aliasing: bool = False,
third_pass: bool = False) -> Type:
raise NotImplementedError

@abstractmethod
def class_type(self, info: TypeInfo) -> Type:
raise NotImplementedError

@abstractmethod
def lookup_fully_qualified(self, name: str) -> SymbolTableNode:
raise NotImplementedError


# A context for a function hook that infers the return type of a function with
# a special signature.
Expand Down Expand Up @@ -262,6 +286,17 @@ def get_method_hook(self, fullname: str
return int_pow_callback
return None

def get_class_decorator_hook(self, fullname: str
) -> Optional[Callable[[ClassDefContext], None]]:
if fullname in mypy.plugins.attrs.attr_class_makers:
return mypy.plugins.attrs.attr_class_maker_callback
elif fullname in mypy.plugins.attrs.attr_dataclass_makers:
return partial(
mypy.plugins.attrs.attr_class_maker_callback,
auto_attribs_default=True
)
return None


def open_callback(ctx: FunctionContext) -> Type:
"""Infer a better return type for 'open'.
Expand Down
Empty file added mypy/plugins/__init__.py
Empty file.
Loading