Skip to content
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

WIP: mypy plugin #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 ex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from glom import glom
from attr import dataclass
from typing import TYPE_CHECKING

if not TYPE_CHECKING:
reveal_type = print


@dataclass
class Person(object):
age: int

person = Person(age=25)
reveal_type(glom(person, 'age'))
Empty file added glom/contrib/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions glom/contrib/mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Type

from mypy.plugin import Plugin, FunctionContext
from mypy.types import Type as MypyType


class _GlomPlugin(Plugin):
def get_function_hook(self, fullname: str) -> MypyType:
if fullname == 'glom.core.glom':
def test(ctx: FunctionContext) -> MypyType:
print(ctx)
print(ctx.arg_types[0][0], ctx.arg_types[1][0])
print(ctx.arg_types[1][0].last_known_value.value)
return ctx.api.expr_checker.analyze_external_member_access(
ctx.arg_types[1][0].last_known_value.value,
ctx.arg_types[0][0],
ctx.context,
)
return test
return None


def plugin(version: str) -> Type[Plugin]:
return _GlomPlugin
22 changes: 22 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[mypy]
# mypy configurations: http://bit.ly/2zEl9WI

# Plugins, includes custom:
plugins =
glom.contrib.mypy

allow_redefinition = False
ignore_errors = False
ignore_missing_imports = True
implicit_reexport = True
strict_optional = True
strict_equality = True
no_implicit_optional = True
local_partial_types = True
warn_no_return = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True

# We need this to debug `mypy` plugin:
show_traceback = True
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ pytest==4.1.1
tox==3.7.0
virtualenv==15.1.0
PyYAML==3.13
mypy==0.780
pytest-mypy-plugins==1.2.0