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

Issue:
Allow yield without argument
The following code results in a stack trace from the type checker:

```
def f():
    yield
```

Fix yield without an argument.



Discussion:
I think this may already be fixed 3de44999 (edit: nevermind, it's a type checker issue, not a parser issue)


Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -947,7 +947,10 @@ def visit_yield_stmt(self, s: YieldStmt) -> Type:
         else:
             self.fail(messages.INVALID_RETURN_TYPE_FOR_YIELD, s)
             return None
-        actual_item_type = self.accept(s.expr, expected_item_type)
+        if s.expr is None:
+            actual_item_type = Void()
+        else:
+            actual_item_type = self.accept(s.expr, expected_item_type)
         self.check_subtype(actual_item_type, expected_item_type, s)
     
     def visit_if_stmt(self, s: IfStmt) -> Type:


Test patch:
diff --git a/mypy/test/data/check-statements.test b/mypy/test/data/check-statements.test
--- a/mypy/test/data/check-statements.test
+++ b/mypy/test/data/check-statements.test
@@ -589,6 +589,12 @@ def f() -> Iterator[int]:
     return
 [builtins fixtures/for.py]
 
+[case testYieldWithNoValue]
+from typing import Iterator
+def f() -> Iterator[None]:
+    yield
+[builtins fixtures/for.py]
+
 
 -- With statement
 -- --------------
Repository: python/mypy

Issue:
Parser gets confused by trailing semicolon
The following fragment is not accepted by the parser, even though it is valid Python:

```
def f():
    pass;    # Note trailing semicolon!
def g():
    pass
```



Discussion:

Resolving patch:
diff --git a/mypy/lex.py b/mypy/lex.py
--- a/mypy/lex.py
+++ b/mypy/lex.py
@@ -632,11 +632,15 @@ def calc_indent(self, s: str) -> int:
     def lex_break(self) -> None:
         """Analyse a line break."""
         s = self.match(self.break_exp)
-        if self.tok and isinstance(self.tok[-1], Break):
-            self.tok[-1].string += self.pre_whitespace + s
+        last_tok = self.tok[-1] if self.tok else None
+        if isinstance(last_tok, Break):
+            was_semicolon = last_tok.string == ';'
+            last_tok.string += self.pre_whitespace + s
             self.i += len(s)
             self.line += 1
             self.pre_whitespace = ''
+            if was_semicolon:
+                self.lex_indent()
         elif self.ignore_break():
             self.add_pre_whitespace(s)
             self.line += 1


Test patch:
diff --git a/mypy/test/data/parse.test b/mypy/test/data/parse.test
--- a/mypy/test/data/parse.test
+++ b/mypy/test/data/parse.test
@@ -2603,3 +2603,20 @@ MypyFile:1(
         NameExpr(x)
         y))
     PassStmt:5()))
+
+[case testTrailingSemicolon]
+def x():
+    pass;
+
+def y():
+    pass
+[out]
+MypyFile:1(
+  FuncDef:1(
+    x
+    Block:1(
+      PassStmt:2()))
+  FuncDef:4(
+    y
+    Block:4(
+      PassStmt:5())))
Repository: python/mypy

Issue:
Do not allow implicit access to internal definitions in builtins
All internal definitions in builtins (such as type variables, aliases) should not be visible outside builtins. Currently, for example, it is possible to refer to `_byte_types`:

```
from typing import Undefined
x = Undefined(_byte_types)   # No error, but will fail at runtime
```



Discussion:

Resolving patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -1351,6 +1351,9 @@ def lookup(self, name: str, ctx: Context) -> SymbolTableNode:
         if b:
             table = cast(MypyFile, b.node).names
             if name in table:
+                if name[0] == "_" and name[1] != "_":
+                    self.name_not_defined(name, ctx)
+                    return None
                 node = table[name]
                 # Only succeed if we are not using a type alias such List -- these must be
                 # be accessed via the typing module.


Test patch:
Repository: python/mypy

Issue:
Refactor overloads away in zipfile stubs
It seems that all the `@overload` decorators in `stubs/3.2/zipfile.py` are redundant and could be replaced with normal functions and union types.



Discussion:

Resolving patch:
diff --git a/stubs/3.2/zipfile.py b/stubs/3.2/zipfile.py
--- a/stubs/3.2/zipfile.py
+++ b/stubs/3.2/zipfile.py
@@ -1,14 +1,11 @@
 # TODO these are incomplete
 
-from typing import overload, List, Undefined, Tuple, BinaryIO
+from typing import List, Undefined, Tuple, BinaryIO, Union
 
 ZIP_STORED = 0
 ZIP_DEFLATED = 0
 
-@overload
-def is_zipfile(filename: str) -> bool: pass
-@overload
-def is_zipfile(filename: BinaryIO) -> bool: pass
+def is_zipfile(filename: Union[str, BinaryIO]) -> bool: pass
 
 class ZipInfo:
     filename = ''
@@ -17,25 +14,14 @@ class ZipInfo:
     file_size = 0
 
 class ZipFile:
-    @overload
-    def __init__(self, file: str, mode: str = 'r',
+    def __init__(self, file: Union[str, BinaryIO], mode: str = 'r',
                  compression: int = ZIP_STORED,
                  allowZip64: bool = False) -> None: pass
-    @overload
-    def __init__(self, file: BinaryIO, mode: str = 'r',
-                  compression: int = ZIP_STORED,
-                 allowZip64: bool = False) -> None: pass
-
     def close(self) -> None: pass
     def getinfo(name: str) -> ZipInfo: pass
     def infolist(self) -> List[ZipInfo]: pass
     def namelist(self) -> List[str]: pass
-
-    @overload
-    def read(self, name: str, pwd: str = None) -> bytes: pass
-    @overload
-    def read(self, name: ZipInfo, pwd: str = None) -> bytes: pass
-
+    def read(self, name: Union[str, ZipInfo], pwd: str = None) -> bytes: pass
     def write(self, filename: str, arcname: str = None,
               compress_type: int = None) -> None: pass
 


Test patch:
Repository: python/mypy

Issue:
Support __call__
Add support for overloading calls by implementing a `__call__` method.



Discussion:

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -206,6 +206,11 @@ def check_call(self, callee: Type, args: List[Node],
             self.msg.disable_type_names -= 1
             return (UnionType.make_simplified_union([res[0] for res in results]),
                     callee)
+        elif isinstance(callee, Instance):
+            call_function = analyse_member_access('__call__', callee, context,
+                                         False, False, self.named_type, self.msg)
+            return self.check_call(call_function, args, arg_kinds, context, arg_names,
+                                   callable_node, arg_messages)
         else:
             return self.msg.not_callable(callee, context), AnyType()
 
diff --git a/mypy/messages.py b/mypy/messages.py
--- a/mypy/messages.py
+++ b/mypy/messages.py
@@ -276,6 +276,8 @@ def has_no_attr(self, typ: Type, member: str, context: Context) -> Type:
         elif member == '__setitem__':
             # Indexed set.
             self.fail('Unsupported target for indexed assignment', context)
+        elif member == '__call__':
+            self.fail('{} not callable'.format(self.format(typ)), context)
         else:
             # The non-special case: a missing ordinary attribute.
             if not self.disable_type_names:


Test patch:
diff --git a/mypy/test/data/check-classes.test b/mypy/test/data/check-classes.test
--- a/mypy/test/data/check-classes.test
+++ b/mypy/test/data/check-classes.test
@@ -1133,6 +1133,27 @@ main, line 8: Argument 1 of "__iadd__" incompatible with "__add__" of supertype
 main, line 8: Signatures of "__iadd__" and "__add__" are incompatible
 
 
+-- Callable objects
+-- ----------------
+
+
+[case testCallableObject]
+import typing
+a = A()
+b = B()
+
+a()  # E: Too few arguments for "__call__" of "A"
+a(a, a)  # E: Too many arguments for "__call__" of "A"
+a = a(a)
+a = a(b)  # E: Argument 1 to "__call__" of "A" has incompatible type "B"; expected "A"
+b = a(a)  # E: Incompatible types in assignment (expression has type "A", variable has type "B")
+
+class A:
+    def __call__(self, x: A) -> A:
+        pass
+class B: pass
+
+
 -- Special cases
 -- -------------
Repository: python/mypy

Issue:
Support 0b int literals
Binary int literals such as `0b0` are not supported.



Discussion:

Resolving patch:
diff --git a/mypy/lex.py b/mypy/lex.py
--- a/mypy/lex.py
+++ b/mypy/lex.py
@@ -394,8 +394,8 @@ def is_at_ellipsis(self) -> bool:
 
     # Regexps used by lex_number
 
-    # Decimal/hex/octal literal or integer complex literal
-    number_exp1 = re.compile('0[xXoO][0-9a-fA-F]+|[0-9]+')
+    # Decimal/hex/octal/binary literal or integer complex literal
+    number_exp1 = re.compile('0[xXoObB][0-9a-fA-F]+|[0-9]+')
     # Float literal, e.g. '1.23' or '12e+34' or '1.2j'
     number_exp2 = re.compile(
         r'[0-9]*\.[0-9]*([eE][-+]?[0-9]+)?|[0-9]+[eE][-+]?[0-9]+')
diff --git a/mypy/parse.py b/mypy/parse.py
--- a/mypy/parse.py
+++ b/mypy/parse.py
@@ -1268,15 +1268,7 @@ def parse_name_expr(self) -> NameExpr:
 
     def parse_int_expr(self) -> IntExpr:
         tok = self.expect_type(IntLit)
-        s = tok.string
-        v = 0
-        if len(s) > 2 and s[1] in 'xX':
-            v = int(s[2:], 16)
-        elif len(s) > 2 and s[1] in 'oO':
-            v = int(s[2:], 8)
-        else:
-            v = int(s)
-        node = IntExpr(v)
+        node = IntExpr(int(tok.string, 0))
         return node
 
     def parse_str_expr(self) -> Node:


Test patch:
diff --git a/mypy/test/data/parse.test b/mypy/test/data/parse.test
--- a/mypy/test/data/parse.test
+++ b/mypy/test/data/parse.test
@@ -1665,8 +1665,8 @@ MypyFile:1(
     Block:1(
       PassStmt:2())))
 
-[case testHexAndOctLiterals]
-0xa, 0Xaf, 0o7, 0O12
+[case testHexOctBinLiterals]
+0xa, 0Xaf, 0o7, 0O12, 0b1, 0B101
 [out]
 MypyFile:1(
   ExpressionStmt:1(
@@ -1674,7 +1674,9 @@ MypyFile:1(
       IntExpr(10)
       IntExpr(175)
       IntExpr(7)
-      IntExpr(10))))
+      IntExpr(10)
+      IntExpr(1)
+      IntExpr(5))))
 
 [case testImportFromWithParens]
 from x import (y)
diff --git a/mypy/test/testlex.py b/mypy/test/testlex.py
--- a/mypy/test/testlex.py
+++ b/mypy/test/testlex.py
@@ -46,6 +46,10 @@ def test_oct_int_literals(self):
         self.assert_lex('0o0 0o127 0O1',
                         'IntLit(0o0) IntLit( 0o127) IntLit( 0O1) ...')
 
+    def test_bin_int_literals(self):
+        self.assert_lex('0b0 0b110 0B1',
+                        'IntLit(0b0) IntLit( 0b110) IntLit( 0B1) ...')
+
     def test_float_literals(self):
         self.assert_lex('1.2 .1 1.',
                         'FloatLit(1.2) FloatLit( .1) FloatLit( 1.) ...')
Repository: python/mypy

Issue:
Don't duplicate mypy version number in documentation
`docs/source/conf.py` should use the mypy version string defined in `mypy/version.py`.



Discussion:

Resolving patch:
diff --git a/docs/source/conf.py b/docs/source/conf.py
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -18,7 +18,9 @@
 # If extensions (or modules to document with autodoc) are in another directory,
 # add these directories to sys.path here. If the directory is relative to the
 # documentation root, use os.path.abspath to make it absolute, like shown here.
-#sys.path.insert(0, os.path.abspath('.'))
+sys.path.insert(0, os.path.abspath('../..'))
+
+from mypy.version import __version__ as mypy_version
 
 # -- General configuration ------------------------------------------------
 
@@ -51,9 +53,9 @@
 # built documents.
 #
 # The short X.Y version.
-version = '0.2'
+version = mypy_version.split('-')[0]
 # The full version, including alpha/beta/rc tags.
-release = '0.2'
+release = mypy_version
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.


Test patch:
Repository: python/mypy

Issue:
Error when a .py file is implied by a package but a .pyi file also exists
I have both p/x.py and p/x.pyi, as well as p/__init.py. When I start mypy as `mypy p/x.py`, things work fine. However when I start it as `mypy p` (using the folder support I added recently) or as `mypy --package p` (which Jukka added last month) I get a complaint about `Module p.x processed twice`.

Apparently it still tries to find p.x using module search, even though it found the file already when traversing the folder/package. It then adds the stub p/x.pyi to its list of things to do, even though the non-stub p/x.py is already in that list.



Discussion:
I have a better theory. It's because the directory traversal finds both. It should prefer the .py over the .pyi I think? I'll make a PR.

It's not obvious which should take precedence. You could argue that if there are both a `.pyi` file and `.py` file in the same directory, the `.pyi` file should take precedence as otherwise the file would always be ignored by both mypy and Python and thus its existence would seem pointless. If mypy would give `.pyi` precedence then the story is pretty logical: Python uses the `.py` file and mypy uses the `.pyi` file.


Resolving patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -959,16 +959,29 @@ def find_modules_recursive(module: str, lib_path: List[str]) -> List[BuildSource
         return []
     result = [BuildSource(module_path, module, None)]
     if module_path.endswith(('__init__.py', '__init__.pyi')):
-        for item in os.listdir(os.path.dirname(module_path)):
+        # Subtle: this code prefers the .pyi over the .py if both
+        # exists, and also prefers packages over modules if both x/
+        # and x.py* exist.  How?  We sort the directory items, so x
+        # comes before x.py and x.pyi.  But the preference for .pyi
+        # over .py is encoded in find_module(); even though we see
+        # x.py before x.pyi, find_module() will find x.pyi first.  We
+        # use hits to avoid adding it a second time when we see x.py.
+        # This also avoids both x.py and x.pyi when x/ was seen first.
+        hits = set()  # type: Set[str]
+        for item in sorted(os.listdir(os.path.dirname(module_path))):
             abs_path = os.path.join(os.path.dirname(module_path), item)
             if os.path.isdir(abs_path) and \
                     (os.path.isfile(os.path.join(abs_path, '__init__.py')) or
                     os.path.isfile(os.path.join(abs_path, '__init__.pyi'))):
+                hits.add(item)
                 result += find_modules_recursive(module + '.' + item, lib_path)
             elif item != '__init__.py' and item != '__init__.pyi' and \
                     item.endswith(('.py', '.pyi')):
-                result += find_modules_recursive(
-                    module + '.' + item.split('.')[0], lib_path)
+                mod = item.split('.')[0]
+                if mod not in hits:
+                    hits.add(mod)
+                    result += find_modules_recursive(
+                        module + '.' + mod, lib_path)
     return result
 
 


Test patch:
Repository: python/mypy

Issue:
Make tuple types subtypes of Sized, Container, Reversible, etc.
Currently `(1, 1)` is compatible with `Sequence[int]` and `Iterable[int]` but not with `Sized` or `Container[int]`, which is inconsistent.

This code should be fine, but currently mypy complains about it:

```
from typing import Sized, Container
def f(x: Sized) -> None: pass
f((1, 2)) # Argument 1 to "f" has incompatible type "Tuple[int, int]"; expected "Sized"
def g(x: Container[int]) -> None: pass
g((1, 2)) # Argument 1 to "g" has incompatible type "Tuple[int, int]"; expected Container[int]
```

Once this has been implemented, also simplify the signature of `len()` to not have `tuple` explicitly (`Sized` should be enough for the argument).



Discussion:

Resolving patch:
diff --git a/mypy/subtypes.py b/mypy/subtypes.py
--- a/mypy/subtypes.py
+++ b/mypy/subtypes.py
@@ -142,6 +142,8 @@ def visit_tuple_type(self, left: TupleType) -> bool:
                 target_item_type = right.args[0]
                 return all(is_subtype(item, target_item_type)
                            for item in left.items)
+            elif is_named_instance(right, 'typing.Sized'):
+                return True
             elif (is_named_instance(right, 'typing.Iterable') or
                   is_named_instance(right, 'typing.Container') or
                   is_named_instance(right, 'typing.Sequence') or


Test patch:
diff --git a/mypy/test/data/check-tuples.test b/mypy/test/data/check-tuples.test
--- a/mypy/test/data/check-tuples.test
+++ b/mypy/test/data/check-tuples.test
@@ -750,3 +750,8 @@ fb(aa) # E: Argument 1 to "fb" has incompatible type "Tuple[A, A]"; expected Tup
 from typing import Container
 a = None  # type: Container[str]
 a = ()
+
+[case testSubtypingTupleIsSized]
+from typing import Sized
+a = None  # type: Sized
+a = ()
diff --git a/mypy/test/data/lib-stub/builtins.py b/mypy/test/data/lib-stub/builtins.py
--- a/mypy/test/data/lib-stub/builtins.py
+++ b/mypy/test/data/lib-stub/builtins.py
@@ -6,7 +6,6 @@ def __init__(self, x) -> None: pass
 
 # These are provided here for convenience.
 class int: pass
-class bool(int): pass
 class str: pass
 
 class tuple: pass
diff --git a/mypy/test/data/lib-stub/typing.py b/mypy/test/data/lib-stub/typing.py
--- a/mypy/test/data/lib-stub/typing.py
+++ b/mypy/test/data/lib-stub/typing.py
@@ -26,7 +26,12 @@
 
 class Container(Generic[T]):
     @abstractmethod
-    def __contains__(self, arg: T) -> bool: ...
+    # Use int because bool isn't in the default test builtins
+    def __contains__(self, arg: T) -> int: pass
+
+class Sized:
+    @abstractmethod
+    def __len__(self) -> int: pass
 
 class Iterable(Generic[T]):
     @abstractmethod
Repository: python/mypy

Issue:
`CompileError` never have message.
```
$ python
>>> from mypy.errors import CompileError
>>> raise CompileError(["Y", "U", "No", "Show", "Message"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
mypy.errors.CompileError
```

(PR on its way, `message` is not passed to `super().__init__()` in  class declaration)



Discussion:

Resolving patch:
diff --git a/mypy/errors.py b/mypy/errors.py
--- a/mypy/errors.py
+++ b/mypy/errors.py
@@ -340,7 +340,7 @@ class CompileError(Exception):
     messages = None  # type: List[str]
 
     def __init__(self, messages: List[str]) -> None:
-        super().__init__()
+        super().__init__('\n'.join(messages))
         self.messages = messages
 
 


Test patch:
Repository: python/mypy

Issue:
mypy crashes when run on a .pyc file
I accidentally ran mypy on a .pyc file and got this error:

```
Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 6, in <module>
    main(__file__)
  File "/usr/local/lib/python3.5/dist-packages/mypy/main.py", line 54, in main
    type_check_only(sources, bin_dir, options)
  File "/usr/local/lib/python3.5/dist-packages/mypy/main.py", line 98, in type_check_only
    python_path=options.python_path)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 200, in build
    content = source.load(lib_path, pyversion)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 110, in load
    return read_program(self.path, pyversion)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 292, in read_program
    text = read_with_python_encoding(path, pyversion)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 1094, in read_with_python_encoding
    return source_bytearray.decode(encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf3 in position 1: ordinal not in range(128)
```

While clearly this is my fault, it's probably the case this should give a friendly error rather than throwing a traceback while parsing.

mypy crashes when run on a .pyc file
I accidentally ran mypy on a .pyc file and got this error:

```
Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 6, in <module>
    main(__file__)
  File "/usr/local/lib/python3.5/dist-packages/mypy/main.py", line 54, in main
    type_check_only(sources, bin_dir, options)
  File "/usr/local/lib/python3.5/dist-packages/mypy/main.py", line 98, in type_check_only
    python_path=options.python_path)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 200, in build
    content = source.load(lib_path, pyversion)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 110, in load
    return read_program(self.path, pyversion)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 292, in read_program
    text = read_with_python_encoding(path, pyversion)
  File "/usr/local/lib/python3.5/dist-packages/mypy/build.py", line 1094, in read_with_python_encoding
    return source_bytearray.decode(encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf3 in position 1: ordinal not in range(128)
```

While clearly this is my fault, it's probably the case this should give a friendly error rather than throwing a traceback while parsing.



Discussion:

Resolving patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -302,6 +302,9 @@ def read_program(path: str, pyversion: Tuple[int, int]) -> str:
     except IOError as ioerr:
         raise CompileError([
             "mypy: can't read file '{}': {}".format(path, ioerr.strerror)])
+    except UnicodeDecodeError as decodeerr:
+        raise CompileError([
+            "mypy: can't decode file '{}': {}".format(path, str(decodeerr))])
     return text
 
 


Test patch:
Repository: python/mypy

Issue:
potential speedup: making `nodes.Context` not an ABC results in a ~20% speedup
Making `nodes.Context` not an ABC results in a 20% speedup (excluding parse time). This is likely because ABCs (and their descendants) have a much slower `isinstance` check.

This looks like low hanging fruit for optimization.



Discussion:

Resolving patch:
diff --git a/mypy/nodes.py b/mypy/nodes.py
--- a/mypy/nodes.py
+++ b/mypy/nodes.py
@@ -14,7 +14,7 @@
 from mypy.util import dump_tagged, short_type
 
 
-class Context(metaclass=ABCMeta):
+class Context:
     """Base type for objects that are valid as error message locations."""
     @abstractmethod
     def get_line(self) -> int: pass


Test patch: