Skip to content

Commit

Permalink
Merge pull request #36 from Shoobx/fallback-to-default-plugin
Browse files Browse the repository at this point in the history
Prefer DefaultPlugin to handle functions
  • Loading branch information
kedder committed Mar 11, 2021
2 parents fd89f19 + 53604e1 commit 77246e3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ All notable changes to this project will be documented in this file.
0.2.12 (unreleased)
-------------------

- Nothing changed yet.
- Better type inference for contextmanagers and open() function: do not override
the default behavior for tese cases


0.2.11 (2021-02-21)
Expand Down
10 changes: 10 additions & 0 deletions src/mypy_zope/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
FunctionLike,
)
from mypy.checker import TypeChecker, is_false_literal
from mypy.options import Options
from mypy.nodes import TypeInfo
from mypy.plugin import (
CheckerPluginInterface,
Expand All @@ -28,6 +29,7 @@
ClassDefContext,
)
from mypy.subtypes import find_member
from mypy.plugins.default import DefaultPlugin

from mypy.nodes import (
Context,
Expand Down Expand Up @@ -91,6 +93,10 @@ def make_simple_type(


class ZopeInterfacePlugin(Plugin):
def __init__(self, options: Options):
super().__init__(options)
self.fallback = DefaultPlugin(options)

def log(self, msg: str) -> None:
if self.options.verbosity >= 1:
print("ZOPE:", msg, file=sys.stderr)
Expand Down Expand Up @@ -121,6 +127,10 @@ def analyze(function_ctx: FunctionContext) -> Type:

return deftype

# Give preference to deault plugin
hook = self.fallback.get_function_hook(fullname)
if hook is not None:
return hook
return analyze

def get_method_signature_hook(
Expand Down
25 changes: 25 additions & 0 deletions tests/samples/contextmanager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from collections import Iterator
from contextlib import contextmanager
from typing import TypeVar, Generic

_T = TypeVar("_T")


class A(Generic[_T]):
pass

@contextmanager
def m(x: _T) -> Iterator[A[_T]]:
...


with m(7) as x:
reveal_type(x)
print(x)


"""
<output>
contextmanager.py:17: note: Revealed type is '__main__.A*[builtins.int*]'
</output>
"""
9 changes: 9 additions & 0 deletions tests/samples/open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
reveal_type(open('str'))
reveal_type(open('str', 'b'))

"""
<output>
open.py:1: note: Revealed type is 'typing.TextIO'
open.py:2: note: Revealed type is 'typing.BinaryIO'
</output>
"""

0 comments on commit 77246e3

Please sign in to comment.