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

Issue:
Mypy considers a nested function a method override
Mypy complains about a nested function that has the same name as a superclass method, even though the message is clearly silly:

```
class A:
    def f(self) -> int: pass

class B(A):
    def g(self) -> None:
        def f(self) -> str: pass  # Return type of "f" incompatible with supertype "A"
```



Discussion:

Resolving patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -468,7 +468,6 @@ def analyze_function(self, defn: FuncItem) -> None:
             defn.type = self.anal_type(defn.type)
             self.check_function_signature(defn)
             if isinstance(defn, FuncDef):
-                defn.info = self.type
                 defn.type = set_callable_name(defn.type, defn)
         for arg in defn.arguments:
             if arg.initializer:


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
@@ -307,6 +307,14 @@ class B(A):
     def __new__(cls) -> int:
         return 1
 
+[case testInnerFunctionNotOverriding]
+class A:
+    def f(self) -> int: pass
+
+class B(A):
+    def g(self) -> None:
+        def f(self) -> str: pass
+
 
 -- Constructors
 -- ------------
diff --git a/mypy/test/data/check-functions.test b/mypy/test/data/check-functions.test
--- a/mypy/test/data/check-functions.test
+++ b/mypy/test/data/check-functions.test
@@ -455,7 +455,7 @@ class A:
 main: note: In function "g":
 main:6: error: Incompatible types in assignment (expression has type "int", variable has type "A")
 main: note: In member "f" of class "A":
-main:8: error: Argument 1 to "g" of "A" has incompatible type "A"; expected "int"
+main:8: error: Argument 1 to "g" has incompatible type "A"; expected "int"
 
 [case testMutuallyRecursiveNestedFunctions]
 def f() -> None:
diff --git a/mypy/test/data/check-generics.test b/mypy/test/data/check-generics.test
--- a/mypy/test/data/check-generics.test
+++ b/mypy/test/data/check-generics.test
@@ -635,6 +635,20 @@ a = A().g(1)
 a = 1
 a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
 
+[case testGenericClassInnerFunctionTypeVariable]
+from typing import TypeVar, Generic
+T = TypeVar('T')
+class A(Generic[T]):
+    def __init__(self, a: T) -> None:
+        self.a = a
+    def f(self, n: int) -> None:
+        def g(a: T):
+            self.a = a
+        g(self.a)
+        g(n) # E: Argument 1 to "g" has incompatible type "int"; expected "T"
+[out]
+main: note: In member "f" of class "A":
+
 
 -- Callable subtyping with generic functions
 -- -----------------------------------------
Repository: python/mypy

Issue:
Pip install on Python 2 doesn't give a useful error message
Now we just get a syntax error.

Pip install on Python 2 doesn't give a useful error message
Now we just get a syntax error.



Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -5,14 +5,14 @@
 import os.path
 import sys
 
-from distutils.core import setup
-from mypy.version import __version__
-from mypy import git
-
 if sys.version_info < (3, 2, 0):
     sys.stderr.write("ERROR: You need Python 3.2 or later to use mypy.\n")
     exit(1)
 
+from distutils.core import setup
+from mypy.version import __version__
+from mypy import git
+
 git.verify_git_integrity_or_abort(".")
 
 version = __version__


Test patch:
Repository: python/mypy

Issue:
no arguments to Optional type leads to index error
``` py
# bar.py
from typing import Optional
def foo():
    # type: () -> Optional
    pass
```

```
$ mypy --fast-parser ./bar.py
...
    self.analyze_function(defn)
  File "/srv/zulip-venv-cache/cd17c4e82877061676869fc515f46b64bf073fa4/zulip-py3-venv/lib/python3.4/site-packages/mypy/semanal.py", line 468, in analyze_function
    defn.type = self.anal_type(defn.type)
  File "/srv/zulip-venv-cache/cd17c4e82877061676869fc515f46b64bf073fa4/zulip-py3-venv/lib/python3.4/site-packages/mypy/semanal.py", line 1038, in anal_type
    return t.accept(a)
  File "/srv/zulip-venv-cache/cd17c4e82877061676869fc515f46b64bf073fa4/zulip-py3-venv/lib/python3.4/site-packages/mypy/types.py", line 496, in accept
    return visitor.visit_callable_type(self)
  File "/srv/zulip-venv-cache/cd17c4e82877061676869fc515f46b64bf073fa4/zulip-py3-venv/lib/python3.4/site-packages/mypy/typeanal.py", line 182, in visit_callable_type
    ret_type=t.ret_type.accept(self),
  File "/srv/zulip-venv-cache/cd17c4e82877061676869fc515f46b64bf073fa4/zulip-py3-venv/lib/python3.4/site-packages/mypy/types.py", line 111, in accept
    return visitor.visit_unbound_type(self)
  File "/srv/zulip-venv-cache/cd17c4e82877061676869fc515f46b64bf073fa4/zulip-py3-venv/lib/python3.4/site-packages/mypy/typeanal.py", line 114, in visit_unbound_type
    return items[0]
IndexError: list index out of range

*** INTERNAL ERROR ***

bar.py:2: error: Internal error -- please report a bug at https://github.com/python/mypy/issues

NOTE: you can use "mypy --pdb ..." to drop into the debugger when this happens.
```



Discussion:
This actually isn't a fast parser problem -- the same crash happens without `--fast-parser`.  I know what's going on here though.


Resolving patch:
diff --git a/mypy/typeanal.py b/mypy/typeanal.py
--- a/mypy/typeanal.py
+++ b/mypy/typeanal.py
@@ -108,6 +108,7 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
             elif fullname == 'typing.Optional':
                 if len(t.args) != 1:
                     self.fail('Optional[...] must have exactly one type argument', t)
+                    return AnyType()
                 items = self.anal_array(t.args)
                 # Currently Optional[t] is just an alias for t.
                 return items[0]


Test patch:
diff --git a/mypy/test/data/semanal-types.test b/mypy/test/data/semanal-types.test
--- a/mypy/test/data/semanal-types.test
+++ b/mypy/test/data/semanal-types.test
@@ -1434,7 +1434,7 @@ MypyFile:1(
 [case testInvalidOptionalType]
 from typing import Optional
 x = 1  # type: Optional[int, str]  # E: Optional[...] must have exactly one type argument
-y = 1  # type: Optional[x]  # E: Invalid type "__main__.x"
+y = 1  # type: Optional  # E: Optional[...] must have exactly one type argument
 [out]
 
 [case testCoAndContravariantTypeVar]
Repository: python/mypy

Issue:
Operations on `None` should fall back to `object`
```
rwbarton@morphism:~/mypy$ python3 -m mypy --strict-optional -c 'None == None'
<string>:1: error: Unsupported left operand type for == (None)
rwbarton@morphism:~/mypy$ python3 -m mypy --strict-optional -c 'str(None)'
<string>:1: error: No overload variant of "str" matches argument types [builtins.None]
```

These should not be errors, as the type of `None` is a subtype of `object`.



Discussion:
Interestingly, this first of these errors isn't just a strict optional problem!

```
$ python3 -m mypy -c 'None == None'
<string>:1: error: Unsupported left operand type for == (None)
```

My renaming of this was incorrect -- currently None is a subtype of object, but object isn't in None's MRO.


Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -1775,9 +1775,14 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
             (isinstance(actual, Instance) and actual.type.fallback_to_any)):
         # These could match anything at runtime.
         return 2
-    if not experiments.STRICT_OPTIONAL and isinstance(actual, NoneTyp):
-        # NoneTyp matches anything if we're not doing strict Optional checking
-        return 2
+    if isinstance(actual, NoneTyp):
+        if not experiments.STRICT_OPTIONAL:
+            # NoneTyp matches anything if we're not doing strict Optional checking
+            return 2
+        else:
+            # NoneType is a subtype of object
+            if isinstance(formal, Instance) and formal.type.fullname() == "builtins.object":
+                return 2
     if isinstance(actual, UnionType):
         return max(overload_arg_similarity(item, formal)
                    for item in actual.items)


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
@@ -245,3 +245,14 @@ def f(b: T) -> T:
 a = None
 b = "foo"
 reveal_type(a and b)  # E: Revealed type is 'builtins.None'
+
+[case testNoneMatchesObjectInOverload]
+import a
+a.f(None)
+
+[file a.pyi]
+from typing import overload
+@overload
+def f() -> None: ...
+@overload
+def f(o: object) -> None: ...
Repository: python/mypy

Issue:
Unused code in erasetype.py: erase_generic_types() and GenericTypeEraser
This function doesn't seem to be called and the class is only used by the function.

@JukkaL is it okay if I delete them? It's really ancient code (much of it from 2012!).

Unused code in erasetype.py: erase_generic_types() and GenericTypeEraser
This function doesn't seem to be called and the class is only used by the function.

@JukkaL is it okay if I delete them? It's really ancient code (much of it from 2012!).



Discussion:
I'm assuming it's fine. Anyone looking for an easy starter issue, go ahead and send a PR!

I'm assuming it's fine. Anyone looking for an easy starter issue, go ahead and send a PR!


Resolving patch:
diff --git a/mypy/erasetype.py b/mypy/erasetype.py
--- a/mypy/erasetype.py
+++ b/mypy/erasetype.py
@@ -80,31 +80,6 @@ def visit_type_type(self, t: TypeType) -> Type:
         return TypeType(t.item.accept(self), line=t.line)
 
 
-def erase_generic_types(t: Type) -> Type:
-    """Remove generic type arguments and type variables from a type.
-
-    Replace all types A[...] with simply A, and all type variables
-    with 'Any'.
-    """
-
-    if t:
-        return t.accept(GenericTypeEraser())
-    else:
-        return None
-
-
-class GenericTypeEraser(TypeTranslator):
-    """Implementation of type erasure"""
-
-    # FIX: What about generic function types?
-
-    def visit_type_var(self, t: TypeVarType) -> Type:
-        return AnyType()
-
-    def visit_instance(self, t: Instance) -> Type:
-        return Instance(t.type, [], t.line)
-
-
 def erase_typevars(t: Type, ids_to_erase: Optional[Container[TypeVarId]] = None) -> Type:
     """Replace all type variables in a type with any,
     or just the ones in the provided collection.


Test patch:
Repository: python/mypy

Issue:
Incorrect check on string interpolation
When sending this type-correct code

``` python
d = {"greeting": "hello", "count": 10}
print("%(greeting)s World. %(count)d%% tests failing" % d)
```

through mypy, I get the following:

```
error: String interpolation mixes specifier with and without mapping keys
```

Which should be accepted; the only interpolator I'm using withot keys is "%%" which is just an escape and can not cause problems



Discussion:
This does look like it's related to the `%%`.  Here's a slightly reduced repro: `"%(foo)s %%" % {"foo": "foo"}`


Resolving patch:
diff --git a/mypy/checkstrformat.py b/mypy/checkstrformat.py
--- a/mypy/checkstrformat.py
+++ b/mypy/checkstrformat.py
@@ -88,7 +88,9 @@ def analyze_conversion_specifiers(self, specifiers: List[ConversionSpecifier],
                                       context: Context) -> bool:
         has_star = any(specifier.has_star() for specifier in specifiers)
         has_key = any(specifier.has_key() for specifier in specifiers)
-        all_have_keys = all(specifier.has_key() for specifier in specifiers)
+        all_have_keys = all(
+            specifier.has_key() or specifier.type == '%' for specifier in specifiers
+        )
 
         if has_key and has_star:
             self.msg.string_interpolation_with_star_and_key(context)
@@ -145,6 +147,9 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
                 mapping[key_str] = self.accept(v)
 
             for specifier in specifiers:
+                if specifier.type == '%':
+                    # %% is allowed in mappings, no checking is required
+                    continue
                 if specifier.key not in mapping:
                     self.msg.key_not_in_mapping(specifier.key, replacements)
                     return


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
@@ -989,6 +989,8 @@ a = None # type: Any
 '%()d' % {'': 2}
 '%(a)d' % {'a': 1, 'b': 2, 'c': 3}
 '%(q)d' % {'a': 1, 'b': 2, 'c': 3}  # E: Key 'q' not found in mapping
+'%(a)d %%' % {'a': 1}
+
 [builtins fixtures/dict.py]
 
 [case testStringInterpolationMappingDictTypes]
Repository: python/mypy

Issue:
--warn-unused-ignores also warns about typeshed
Note sure whether that's expected or a workaround like for #1554 is needed:
`/usr/lib/mypy/typeshed/stdlib/2and3/logging/handlers.pyi:107: note: unused 'type: ignore' comment`



Discussion:
That one is actually needed because of #698, and the ignore is not unused when `--strict-optional` is given. So I think we should just live with it until #698 is fixed and then we can remove it.

It's also worth putting in the same workaround as in #1554 for now.


Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -145,8 +145,7 @@ def visit_file(self, file_node: MypyFile, path: str) -> None:
         self.globals = file_node.names
         self.weak_opts = file_node.weak_opts
         self.enter_partial_types()
-        # gross, but no other clear way to tell
-        self.is_typeshed_stub = self.is_stub and 'typeshed' in os.path.normpath(path).split(os.sep)
+        self.is_typeshed_stub = self.errors.is_typeshed_file(path)
 
         for d in file_node.defs:
             self.accept(d)
diff --git a/mypy/errors.py b/mypy/errors.py
--- a/mypy/errors.py
+++ b/mypy/errors.py
@@ -202,12 +202,17 @@ def add_error_info(self, info: ErrorInfo) -> None:
 
     def generate_unused_ignore_notes(self) -> None:
         for file, ignored_lines in self.ignored_lines.items():
-            for line in ignored_lines - self.used_ignored_lines[file]:
-                # Don't use report since add_error_info will ignore the error!
-                info = ErrorInfo(self.import_context(), file, None, None,
-                                 line, 'note', "unused 'type: ignore' comment",
-                                 False, False)
-                self.error_info.append(info)
+            if not self.is_typeshed_file(file):
+                for line in ignored_lines - self.used_ignored_lines[file]:
+                    # Don't use report since add_error_info will ignore the error!
+                    info = ErrorInfo(self.import_context(), file, None, None,
+                                    line, 'note', "unused 'type: ignore' comment",
+                                    False, False)
+                    self.error_info.append(info)
+
+    def is_typeshed_file(self, file: str) -> bool:
+        # gross, but no other clear way to tell
+        return 'typeshed' in os.path.normpath(file).split(os.sep)
 
     def num_messages(self) -> int:
         """Return the number of generated messages."""


Test patch:
Repository: python/mypy

Issue:
Conditional expression with branches of type None and T should be Optional[T], not object
Consider:

``` python
def f(flag; bool) -> Optional[int]:
  a = (None if flag else 0)
  return a
```

When run with `--strict-optional` I get the error

```
error: Incompatible return value type (got "object", expected "Optional[int]")
```

It looks like the unification code is not understanding what's going on and falling back on `object`.



Discussion:
Related to #1863.  This is due to mypy's reluctance to make Union types.

As discussed under #1863, we should go ahead and become willing to make Union types when one of the types to be combined is None. We might go back later and become less reluctant to make Union types more broadly, but there's more detailed discussion to be had there (see #1863.)


Resolving patch:
diff --git a/mypy/join.py b/mypy/join.py
--- a/mypy/join.py
+++ b/mypy/join.py
@@ -66,6 +66,9 @@ def join_types(s: Type, t: Type) -> Type:
     if isinstance(s, ErasedType):
         return t
 
+    if isinstance(s, UnionType) and not isinstance(t, UnionType):
+        s, t = t, s
+
     if isinstance(s, NoneTyp) and not isinstance(t, NoneTyp):
         s, t = t, s
 
@@ -114,8 +117,12 @@ def visit_none_type(self, t: NoneTyp) -> Type:
         if experiments.STRICT_OPTIONAL:
             if isinstance(self.s, (NoneTyp, UninhabitedType)):
                 return t
+            elif isinstance(self.s, UnboundType):
+                return AnyType()
+            elif isinstance(self.s, Void) or isinstance(self.s, ErrorType):
+                return ErrorType()
             else:
-                return self.default(self.s)
+                return UnionType.make_simplified_union([self.s, t])
         else:
             if not isinstance(self.s, Void):
                 return self.s


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
@@ -330,3 +330,11 @@ def lookup_field(name, obj):
 [out]
 main: note: In function "lookup_field":
 main:10: error: Need type annotation for variable
+
+[case testTernaryWithNone]
+reveal_type(None if bool() else 0)  # E: Revealed type is 'Union[builtins.int, builtins.None]'
+[builtins fixtures/bool.py]
+
+[case testListWithNone]
+reveal_type([0, None, 0])    # E: Revealed type is 'builtins.list[Union[builtins.int, builtins.None]]'
+[builtins fixtures/list.py]
Repository: python/mypy

Issue:
`' '` is not accepted as a valid format specifier key name
When type-checking the following code:

``` python
print("%( )s world" % {' ': 'hello'})
```

`mypy` gives the error:

```
error: Unsupported format character '('
```

Sounds like mypy doesn't currently accept spaces as the keys for format specifiers.



Discussion:
Looks like it's not the '(' so much as that it doesn't believe that the
thing between '(' and ')' can contain whitespace?


Resolving patch:
diff --git a/mypy/checkstrformat.py b/mypy/checkstrformat.py
--- a/mypy/checkstrformat.py
+++ b/mypy/checkstrformat.py
@@ -70,7 +70,7 @@ def check_str_interpolation(self, str: StrExpr, replacements: Node) -> Type:
         return self.named_type('builtins.str')
 
     def parse_conversion_specifiers(self, format: str) -> List[ConversionSpecifier]:
-        key_regex = r'(\((\w*)\))?'  # (optional) parenthesised sequence of characters
+        key_regex = r'(\(([\w ]*)\))?'  # (optional) parenthesised sequence of characters
         flags_regex = r'([#0\-+ ]*)'  # (optional) sequence of flags
         width_regex = r'(\*|[1-9][0-9]*)?'  # (optional) minimum field width (* or numbers)
         precision_regex = r'(?:\.(\*|[0-9]+)?)?'  # (optional) . followed by * of numbers


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
@@ -1108,6 +1108,8 @@ di = None # type: Dict[int, int]
 main:3: error: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]")
 main:4: error: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]")
 
+[case testStringInterpolationSpaceKey]
+'%( )s' % {' ': 'foo'}
 
 -- Lambdas
 -- -------
Repository: python/mypy

Issue:
Invalid type comment in Python 2 may crash with fast parser
This crashes when checked in Python 2 mode with the fast parser:

```
from typing import Sequence

def f(a):
  # type: (Sequence(int)) -> int
  pass
```

(The error is that it uses `()` instead of `[]` for the generic type.)

I also have a fix ready.



Discussion:

Resolving patch:
diff --git a/mypy/fastparse2.py b/mypy/fastparse2.py
--- a/mypy/fastparse2.py
+++ b/mypy/fastparse2.py
@@ -39,7 +39,7 @@
 from mypy import defaults
 from mypy import experiments
 from mypy.errors import Errors
-from mypy.fastparse import TypeConverter
+from mypy.fastparse import TypeConverter, TypeCommentParseError
 
 try:
     from typed_ast import ast27
@@ -866,9 +866,3 @@ def visit_ExtSlice(self, n: ast27.ExtSlice) -> Node:
     # Index(expr value)
     def visit_Index(self, n: ast27.Index) -> Node:
         return self.visit(n.value)
-
-
-class TypeCommentParseError(Exception):
-    def __init__(self, msg: str, lineno: int) -> None:
-        self.msg = msg
-        self.lineno = lineno


Test patch:
diff --git a/test-data/unit/check-fastparse.test b/test-data/unit/check-fastparse.test
--- a/test-data/unit/check-fastparse.test
+++ b/test-data/unit/check-fastparse.test
@@ -170,3 +170,12 @@ def f(x):  # E: Type signature has too few arguments
     pass
 [out]
 main: note: In function "f":
+
+[case testFasterParseTypeCommentError_python2]
+# flags: --fast-parser
+from typing import Tuple
+def f(a):
+    # type: (Tuple(int, int)) -> int
+    pass
+[out]
+main:3: error: invalid type comment
Repository: python/mypy

Issue:
Fast parser mode doesn't process import __builtin__ correctly (in Python 2 mode)
See https://github.com/python/typeshed/pull/569 and https://github.com/python/typeshed/pull/570. The former passed the tests without fast parser but would have failed with it. A simple repro is this:

```
mypy --py2 --fast-parser -c 'import __builtin__; s = __builtin__.str'
```

This fails with

```
<string>:1: error: Name '__builtin__' is not defined
```

It passes without `--fast-parser`. It should pass either way.



Discussion:

Resolving patch:
diff --git a/mypy/fastparse.py b/mypy/fastparse.py
--- a/mypy/fastparse.py
+++ b/mypy/fastparse.py
@@ -496,7 +496,17 @@ def visit_Assert(self, n: ast35.Assert) -> Node:
     # Import(alias* names)
     @with_line
     def visit_Import(self, n: ast35.Import) -> Node:
-        i = Import([(self.translate_module_id(a.name), a.asname) for a in n.names])
+        names = []  # type: List[Tuple[str, str]]
+        for alias in n.names:
+            name = self.translate_module_id(alias.name)
+            asname = alias.asname
+            if asname is None and name != alias.name:
+                # if the module name has been translated (and it's not already
+                # an explicit import-as), make it an implicit import-as the
+                # original name
+                asname = alias.name
+            names.append((name, asname))
+        i = Import(names)
         self.imports.append(i)
         return i
 
diff --git a/mypy/fastparse2.py b/mypy/fastparse2.py
--- a/mypy/fastparse2.py
+++ b/mypy/fastparse2.py
@@ -572,7 +572,17 @@ def visit_Assert(self, n: ast27.Assert) -> Node:
     # Import(alias* names)
     @with_line
     def visit_Import(self, n: ast27.Import) -> Node:
-        i = Import([(self.translate_module_id(a.name), a.asname) for a in n.names])
+        names = []  # type: List[Tuple[str, str]]
+        for alias in n.names:
+            name = self.translate_module_id(alias.name)
+            asname = alias.asname
+            if asname is None and name != alias.name:
+                # if the module name has been translated (and it's not already
+                # an explicit import-as), make it an implicit import-as the
+                # original name
+                asname = alias.name
+            names.append((name, asname))
+        i = Import(names)
         self.imports.append(i)
         return i
 


Test patch:
diff --git a/test-data/unit/check-python2.test b/test-data/unit/check-python2.test
--- a/test-data/unit/check-python2.test
+++ b/test-data/unit/check-python2.test
@@ -213,3 +213,8 @@ def bar(key: Callable[[Tuple[int, int]], int]) -> int:
 def foo() -> int:
     return bar(key=lambda (a, b): a)
 [out]
+
+[case testImportBuiltins]
+# flags: --fast-parser
+import __builtin__
+__builtin__.str