internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: python/mypy

Issue:
mypy master fails in typeshed CI
```
  File "/home/travis/virtualenv/python3.5.2/lib/python3.5/site-packages/mypy/nodes.py", line 2275, in SymbolTableNode
    def type(self) -> Optional['mypy.types.Type']:
  File "/opt/python/3.5.2/lib/python3.5/typing.py", line 649, in __getitem__
    return Union[arg, type(None)]
  File "/opt/python/3.5.2/lib/python3.5/typing.py", line 552, in __getitem__
    dict(self.__dict__), parameters, _root=True)
  File "/opt/python/3.5.2/lib/python3.5/typing.py", line 512, in __new__
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
  File "/opt/python/3.5.2/lib/python3.5/typing.py", line 512, in <genexpr>
    for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
  File "/opt/python/3.5.2/lib/python3.5/typing.py", line 190, in __subclasscheck__
    self._eval_type(globalns, localns)
  File "/opt/python/3.5.2/lib/python3.5/typing.py", line 177, in _eval_type
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
AttributeError: module 'mypy' has no attribute 'types'
```
From https://travis-ci.org/python/typeshed/jobs/227972982

Presumably we need to use `'Optional[mypy.types.Type]'` instead? Not sure how this wasn't caught by any of mypy's own tests.


Discussion:
Just traded this back to Ivan's recent Optional cleanup. More to follow.
This is definitely due to ``Optional['mypy.types.Type']``. But this (eager union simplification using forward reference evaluation) was fixed several month ago in ``typing``. Do we need to support also older versions of ``typing``? If yes, then I can make a small PR replacing this with ``'Optional[mypy.types.Type]'``.
Yes, we need to support whatever typing.py is shipped with Python 3.5.1. #ilevkivskyi how soon can you have this fixed? Otherwise I'll just revert #3292 (the culprit).
I can do this within 30 minutes.

Resolving patch:
diff --git a/mypy/nodes.py b/mypy/nodes.py
--- a/mypy/nodes.py
+++ b/mypy/nodes.py
@@ -2272,7 +2272,7 @@ def fullname(self) -> Optional[str]:
             return None
 
     @property
-    def type(self) -> Optional['mypy.types.Type']:
+    def type(self) -> 'Optional[mypy.types.Type]':
         # IDEA: Get rid of the Any type.
         node = self.node  # type: Any
         if self.type_override is not None:


Test patch:
Repository: python/mypy

Issue:
mypy_extensions is listed as owned by David Foster
See https://github.com/python/mypy/blob/master/extensions/setup.py#L37

David Foster did indeed create the first version but I presume he doesn't want to be bothered about the subsequent additions?

We should probably change this to "The mypy developers" -- but where to point the email? Maybe it can be omitted. The url might also better point to GitHub.

Attn: @davidfstr 


Discussion:
+1 to set the contact name & email more appropriately. I wasn't sure what to set it to when I created the package originally.

Resolving patch:
diff --git a/extensions/setup.py b/extensions/setup.py
--- a/extensions/setup.py
+++ b/extensions/setup.py
@@ -4,7 +4,7 @@
 
 from distutils.core import setup
 
-version = '0.2.0-dev'
+version = '0.2.0'
 description = 'Experimental type system extensions for programs checked with the mypy typechecker.'
 long_description = '''
 Mypy Extensions
@@ -26,6 +26,7 @@
     'Programming Language :: Python :: 3.3',
     'Programming Language :: Python :: 3.4',
     'Programming Language :: Python :: 3.5',
+    'Programming Language :: Python :: 3.6',
     'Topic :: Software Development',
 ]
 
@@ -34,8 +35,8 @@
     version=version,
     description=description,
     long_description=long_description,
-    author='David Foster',
-    author_email='david@dafoster.net',
+    author='The mypy developers',
+    author_email='jukka.lehtosalo@iki.fi',
     url='http://www.mypy-lang.org/',
     license='MIT License',
     platforms=['POSIX'],


Test patch:
Repository: python/mypy

Issue:
Slice index false positive

```
from typing import Union, Any

d: Union[int, None]
q: Any
q[0:d]  # error: Slice index must be an integer or None
```

Reported by @chrish42.


Discussion:
Probably a typeshed issue.
I don't think so, this is mypy's general slicing check, not a specific `__getitem__` override. Looks like the bug is in `visit_slice_expr` in checkexpr.py. The error message says that int or None is accepted, but the code only accepts int.
Furthermore, None as a slice index works in at least some cases:

```
>>> range(10)[3:None]
range(3, 10)
```

So, at least for some cases, the code in `visit_slice_expr` should be more lenient and accept None as a slice index.
OK, I thought mypy usually eventually defer to a dunder method, but if it doesn't in this case it's a mypy bug, perhaps related to strict optional.

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -45,6 +45,7 @@
 from mypy.typevars import fill_typevars
 from mypy.visitor import ExpressionVisitor
 from mypy.funcplugins import get_function_plugin_callbacks, PluginCallback
+from mypy.typeanal import make_optional_type
 
 from mypy import experiments
 
@@ -1928,10 +1929,11 @@ def analyze_super(self, e: SuperExpr, is_lvalue: bool) -> Type:
             return AnyType()
 
     def visit_slice_expr(self, e: SliceExpr) -> Type:
+        expected = make_optional_type(self.named_type('builtins.int'))
         for index in [e.begin_index, e.end_index, e.stride]:
             if index:
                 t = self.accept(index)
-                self.chk.check_subtype(t, self.named_type('builtins.int'),
+                self.chk.check_subtype(t, expected,
                                        index, messages.INVALID_SLICE_INDEX)
         return self.named_type('builtins.slice')
 


Test patch:
diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test
--- a/test-data/unit/check-expressions.test
+++ b/test-data/unit/check-expressions.test
@@ -1006,6 +1006,16 @@ a[None:]
 a[:None]
 [builtins fixtures/slice.pyi]
 
+[case testNoneSliceBoundsWithStrictOptional]
+# flags: --strict-optional
+from typing import Any
+a = None # type: Any
+a[None:1]
+a[1:None]
+a[None:]
+a[:None]
+[builtins fixtures/slice.pyi]
+
 
 -- String interpolation
 -- --------------------
Repository: python/mypy

Issue:
`Any` apparently not considered part of `Union[Any, builtins.None]`
When running `mypy --strict` against the following code:

```py
from typing import Any, Optional

class Test(object):

    def __init__(self) -> None:
        self.attr = "foo"  # type: Any

    def foo(self, do_it: bool) -> Optional[Any]:
        if do_it:
            return self.attr
        else:
            return None
```

I see the following warning:

```
testcase.py:11: warning: Returning Any from function with declared return type "Union[Any, builtins.None]"
```

This seems obviously incorrect. At the very least, it's a confusing error message.

Bisecting suggests that this behaviour was introduced in 4141d2039c709f3a8e591fa35a560e54c4e203d2.


Discussion:
That's @JukkaL's PR #3086.
I think the problem is not with Jukka's PR but in the logic of ``--warn-return-any``. It should not emit warning if ``Any`` is a proper subtype of the declared return type. I have an idea for a very simple fix, if it will work, then I will make a PR shortly.

Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -1874,7 +1874,8 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
                 if isinstance(typ, AnyType):
                     # (Unless you asked to be warned in that case, and the
                     # function is not declared to return Any)
-                    if not isinstance(return_type, AnyType) and self.options.warn_return_any:
+                    if (not is_proper_subtype(AnyType(), return_type) and
+                            self.options.warn_return_any):
                         self.warn(messages.RETURN_ANY.format(return_type), s)
                     return
 


Test patch:
diff --git a/test-data/unit/check-warnings.test b/test-data/unit/check-warnings.test
--- a/test-data/unit/check-warnings.test
+++ b/test-data/unit/check-warnings.test
@@ -165,3 +165,20 @@ from typing import Any
 def g() -> Any: pass
 def f() -> Any: return g()
 [out]
+
+[case testOKReturnAnyIfProperSubtype]
+# flags: --warn-return-any --strict-optional
+from typing import Any, Optional
+
+class Test(object):
+
+    def __init__(self) -> None:
+        self.attr = "foo"  # type: Any
+
+    def foo(self, do_it: bool) -> Optional[Any]:
+        if do_it:
+            return self.attr  # Should not warn here
+        else:
+            return None
+[builtins fixtures/list.pyi]
+[out]
Repository: python/mypy

Issue:
Error in Cycle Expansion when running html-report
When running `--html-report` on mypy project, it outputs: `ERROR: cycle in function expansion; skipping` for some functions. We should figure out where this error comes from and fix it.

This is the full output:
```
mypy$ mypy --html-report ../report mypy
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
ERROR: cycle in function expansion; skipping
Generated HTML report (via XSLT): /Users/stas/Projects/report/index.html
```


Discussion:

Resolving patch:
diff --git a/mypy/stats.py b/mypy/stats.py
--- a/mypy/stats.py
+++ b/mypy/stats.py
@@ -57,7 +57,7 @@ def __init__(self, inferred: bool, typemap: Dict[Expression, Type] = None,
 
     def visit_func_def(self, o: FuncDef) -> None:
         self.line = o.line
-        if len(o.expanded) > 1:
+        if len(o.expanded) > 1 and o.expanded != [o] * len(o.expanded):
             if o in o.expanded:
                 print('ERROR: cycle in function expansion; skipping')
                 return


Test patch:
Repository: python/mypy

Issue:
Value incorrectly flagged as being possibly None after `or` predicate ensures it isn't None
Invoked with: ` python3.6 -m mypy --warn-no-return --strict-optional test_or_shortcut.py`

```python
from typing import Optional, Any


def test_or_shortcut(value: Optional[Any]) -> None:
    if not value:
        pass
    if not value or value.get('foo') == 'hello':
        pass
```
Fails with:
```
test_or_shortcut.py:7: error: Item "None" of "Optional[Any]" has no attribute "get"
```

Oddly if I remove the first branch things typecheck fine:

```python
from typing import Optional, Any


def test_or_shortcut(value: Optional[Any]) -> None:
    if not value or value.get('foo') == 'hello':
        pass
```


Discussion:
Hm, that looks like a binder bug. But I can't repro it. Does it still fail this way in master?
@gvanrossum Repro'd on HEAD:

```bash
rwilliams at rwilliams-mbp114-2 in ~/src/oss/mypy (master●) (venv)
$ python3.6 -m mypy -V
mypy 0.520-dev-72168faf682722ff8ffa6876d3c2fb94a30d5012
```
Ah, I did'nt copy the flags you are passing. Sorry. Labeled as bug affecting strict-optional.
This looks familiar to me -- I think this might be to do with `Any`s behavior in the subtyping check (or maybe with `is_subtype` being used instead of `is_proper_subtype`).
I was wrong -- it's actually related to truthiness tracking.  For some reason, after the `if not value: pass`, the `Any` part of the union has `can_be_true` set to `False`.
What if there is something other than `pass` in the first `if` block?
I was wondering that too.  I tried replacing the `pass` with an unrelated assignment statement -- the problem persisted.
Okay, I think I've tracked this down to the root cause of the problem: `true_or_false` (in `types.py`) doesn't behave correctly on `Union`s.  In particular, it sets `can_be_true` and `can_be_false` to `True` on the *`Union` type*, but not on any of its items!

Resolving patch:
diff --git a/mypy/types.py b/mypy/types.py
--- a/mypy/types.py
+++ b/mypy/types.py
@@ -1699,6 +1699,10 @@ def true_or_false(t: Type) -> Type:
     """
     Unrestricted version of t with both True-ish and False-ish values
     """
+    if isinstance(t, UnionType):
+        new_items = [true_or_false(item) for item in t.items]
+        return UnionType.make_simplified_union(new_items, line=t.line, column=t.column)
+
     new_t = copy_type(t)
     new_t.can_be_true = type(new_t).can_be_true
     new_t.can_be_false = type(new_t).can_be_false


Test patch:
diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test
--- a/test-data/unit/check-optional.test
+++ b/test-data/unit/check-optional.test
@@ -594,3 +594,12 @@ x: Optional[Union[int, str]]
 reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.None]'
 y: Optional[Union[int, None]]
 reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]'
+
+[case testUnionTruthinessTracking]
+from typing import Optional, Any
+def test_or_shortcut(value: Optional[Any]) -> None:
+    if not value:
+        pass
+    if not value or value.get('foo') == 'hello':
+        pass
+[builtins fixtures/bool.pyi]
Repository: python/mypy

Issue:
Overload resolution fails for Type[T] and metaclass
Given this code:

```python
from typing import Iterable, Iterator, List, Type

class EM(type, Iterable[int]):
    def __iter__(self) -> Iterator[int]:
        return iter(range(5))

class E(metaclass=EM):
    pass

def passes_typecheck(e: EM) -> List[int]:
    return list(e)

def fails_typecheck(e: Type[E]) -> List[int]:
    return list(e)

print(passes_typecheck(E))
print(fails_typecheck(E))
```

mypy fails type checking for the `fails_typecheck` call with the following error:

```
bug.py:14: error: No overload variant of "list" matches argument types [Type[bug.E]]
```

Given that metaclasses are sometimes an implementation detail, I think this should be supported, however this could just be ignorance on my part.


Discussion:
This looks like a bug. 

#3326 is related but probably a separate issue.
This is a problem in the treatment of overload resolution.

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -2651,8 +2651,12 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
             else:
                 return 0
         elif isinstance(actual, TypeType):
+            item = actual.item
             if formal.type.fullname() in {"builtins.object", "builtins.type"}:
                 return 2
+            elif isinstance(item, Instance):
+                # FIX: this does not handle e.g. Union of instances
+                return overload_arg_similarity(item.type.metaclass_type, formal)
             else:
                 return 0
         else:


Test patch:
diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test
--- a/test-data/unit/check-classes.test
+++ b/test-data/unit/check-classes.test
@@ -3290,6 +3290,38 @@ x3: M = cv
 
 [builtins fixtures/classmethod.pyi]
 
+[case testMetaclassOverloadResolution]
+from typing import Type, overload
+class A: pass
+
+class EM(type): pass
+class E(metaclass=EM): pass
+
+class EM1(type): pass
+class E1(metaclass=EM1): pass
+
+@overload
+def f(x: EM) -> int: ...
+@overload
+def f(x: EM1) -> A: ...
+@overload
+def f(x: str) -> str: ...
+def f(x: object) -> object: return ''
+
+e: EM
+reveal_type(f(e))  # E: Revealed type is 'builtins.int'
+
+et: Type[E]
+reveal_type(f(et))  # E: Revealed type is 'builtins.int'
+
+e1: EM1
+reveal_type(f(e1))  # E: Revealed type is '__main__.A'
+
+e1t: Type[E1]
+reveal_type(f(e1t))  # E: Revealed type is '__main__.A'
+
+reveal_type(f(''))  # E: Revealed type is 'builtins.str'
+
 -- Synthetic types crashes
 -- -----------------------
Repository: python/mypy

Issue:
ZeroDivisionError in --any-exprs-report if file contains no expressions
Example:
```
(v36) guido-mbp:mypy guido$ mypy --any-exprs-report=out -c 'pass'
Traceback (most recent call last):
  File "/Users/guido/v36/bin/mypy", line 6, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/Users/guido/src/mypy/scripts/mypy", line 6, in <module>
    main(__file__)
  File "/Users/guido/src/mypy/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/guido/src/mypy/mypy/main.py", line 97, in type_check_only
    options=options)
  File "/Users/guido/src/mypy/mypy/build.py", line 198, in build
    reports.finish()
  File "/Users/guido/src/mypy/mypy/report.py", line 66, in finish
    reporter.on_finish()
  File "/Users/guido/src/mypy/mypy/report.py", line 167, in on_finish
    total_coverage = (float(total_expr - total_any) / float(total_expr)) * 100
ZeroDivisionError: float division by zero
```


Discussion:

Resolving patch:
diff --git a/mypy/report.py b/mypy/report.py
--- a/mypy/report.py
+++ b/mypy/report.py
@@ -164,7 +164,9 @@ def on_file(self,
     def on_finish(self) -> None:
         total_any = sum(num_any for num_any, _ in self.counts.values())
         total_expr = sum(total for _, total in self.counts.values())
-        total_coverage = (float(total_expr - total_any) / float(total_expr)) * 100
+        total_coverage = 100.0
+        if total_expr > 0:
+            total_coverage = (float(total_expr - total_any) / float(total_expr)) * 100
 
         any_column_name = "Anys"
         total_column_name = "Exprs"


Test patch:
diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test
--- a/test-data/unit/cmdline.test
+++ b/test-data/unit/cmdline.test
@@ -259,6 +259,9 @@ strict_optional = True
 [out]
 mypy.ini: [mypy-*]: Per-module sections should only specify per-module flags (strict_optional)
 
+[case testAnyExprReportDivisionByZero]
+# cmd: mypy --any-exprs-report=out -c 'pass'
+
 [case testCoberturaParser]
 # cmd: mypy --cobertura-xml-report build pkg
 [file pkg/__init__.py]
Repository: python/mypy

Issue:
Typing for @contextmanager doesn't play well with generic functions
```
from contextlib import contextmanager
from typing import TypeVar, Iterator

_T = TypeVar('_T')

@contextmanager
def yield_id(item):
    # type: (_T) -> Iterator[_T]
    yield item

with yield_id(1):
    pass
```

... results in...

`example.py:11: error: Argument 1 to "yield_id" has incompatible type "int"; expected "_T"`



Discussion:

Resolving patch:
diff --git a/mypy/plugin.py b/mypy/plugin.py
--- a/mypy/plugin.py
+++ b/mypy/plugin.py
@@ -247,7 +247,9 @@ def contextmanager_callback(ctx: FunctionContext) -> Type:
             return ctx.default_return_type.copy_modified(
                 arg_types=arg_type.arg_types,
                 arg_kinds=arg_type.arg_kinds,
-                arg_names=arg_type.arg_names)
+                arg_names=arg_type.arg_names,
+                variables=arg_type.variables,
+                is_ellipsis_args=arg_type.is_ellipsis_args)
     return ctx.default_return_type
 
 


Test patch:
diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py
--- a/mypy/test/testcheck.py
+++ b/mypy/test/testcheck.py
@@ -77,6 +77,7 @@
     'check-enum.test',
     'check-incomplete-fixture.test',
     'check-custom-plugin.test',
+    'check-default-plugin.test',
 ]
 
 
diff --git a/test-data/unit/check-default-plugin.test b/test-data/unit/check-default-plugin.test
new file mode 100644
--- /dev/null
+++ b/test-data/unit/check-default-plugin.test
@@ -0,0 +1,33 @@
+-- Test cases for the default plugin
+--
+-- Note that we have additional test cases in pythoneval.test (that use real typeshed stubs).
+
+
+[case testContextManagerWithGenericFunction]
+from contextlib import contextmanager
+from typing import TypeVar, Iterator
+
+T = TypeVar('T')
+
+@contextmanager
+def yield_id(item: T) -> Iterator[T]:
+    yield item
+
+reveal_type(yield_id) # E: Revealed type is 'def [T] (item: T`-1) -> contextlib.GeneratorContextManager[T`-1]'
+
+with yield_id(1) as x:
+    reveal_type(x) # E: Revealed type is 'builtins.int*'
+
+f = yield_id
+def g(x, y): pass
+f = g # E: Incompatible types in assignment (expression has type Callable[[Any, Any], Any], variable has type Callable[[T], GeneratorContextManager[T]])
+[typing fixtures/typing-full.pyi]
+
+[case testContextManagerWithUnspecifiedArguments]
+from contextlib import contextmanager
+from typing import Callable, Iterator
+
+c: Callable[..., Iterator[int]]
+reveal_type(c) # E: Revealed type is 'def (*Any, **Any) -> typing.Iterator[builtins.int]'
+reveal_type(contextmanager(c)) # E: Revealed type is 'def (*Any, **Any) -> contextlib.GeneratorContextManager[builtins.int*]'
+[typing fixtures/typing-full.pyi]
Repository: python/mypy

Issue:
Generic constraint inference problem with TypedDict
Mypy doesn't seem to infer constraints correctly for TypedDicts. Example:

```py
from typing import TypeVar, Iterable
from mypy_extensions import TypedDict

T = TypeVar('T')

def f(x: Iterable[T]) -> T: pass

A = TypedDict('A', {'x': int})

a: A
reveal_type(f(a)) # Argument 1 to "f" has incompatible type "A"; expected Iterable[<nothing>]
```

The `f(a)` call should be okay and the revealed type should be `str`. This affects `iter()` at least.


Discussion:

Resolving patch:
diff --git a/mypy/constraints.py b/mypy/constraints.py
--- a/mypy/constraints.py
+++ b/mypy/constraints.py
@@ -312,6 +312,8 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
         res = []  # type: List[Constraint]
         if isinstance(actual, CallableType) and actual.fallback is not None:
             actual = actual.fallback
+        if isinstance(actual, TypedDictType):
+            actual = actual.as_anonymous().fallback
         if isinstance(actual, Instance):
             instance = actual
             if (self.direction == SUBTYPE_OF and


Test patch:
diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test
--- a/test-data/unit/check-typeddict.test
+++ b/test-data/unit/check-typeddict.test
@@ -557,6 +557,17 @@ reveal_type(f(g)) # E: Revealed type is '<nothing>'
 
 -- Constraint Solver
 
+[case testTypedDictConstraintsAgainstIterable]
+from typing import TypeVar, Iterable
+from mypy_extensions import TypedDict
+T = TypeVar('T')
+def f(x: Iterable[T]) -> T: pass
+A = TypedDict('A', {'x': int})
+a: A
+reveal_type(f(a)) # E: Revealed type is 'builtins.str*'
+[builtins fixtures/dict.pyi]
+[typing fixtures/typing-full.pyi]
+
 -- TODO: Figure out some way to trigger the ConstraintBuilderVisitor.visit_typeddict_type() path.