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

Issue:
Protocol requires __slots__ to be explicitly typed
Bug report. Mypy version: mypy 0.730+dev.445020f0ed9939b43371edbddcf5b083a706d6d8

**Code:**
```
class A(Protocol):
    __slots__ = ()
```

**Expected:**

Passes, and `__slots__` is not part of the Protocol interface. It's just being provided to prevent subclasses from being forced to have a `__dict__`.

**Actual:**
```
error: All protocol members must have explicitly declared types
```


Discussion:
This is a definitely a bug, though should be easy to work around with a `type: ignore`. It should be pretty easy to fix if you want to take a look

Resolving patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -2320,8 +2320,8 @@ def process_type_annotation(self, s: AssignmentStmt) -> None:
                 if isinstance(lvalue.node, Var):
                     lvalue.node.is_abstract_var = True
         else:
-            if (any(isinstance(lv, NameExpr) and lv.is_inferred_def for lv in s.lvalues) and
-                    self.type and self.type.is_protocol and not self.is_func_scope()):
+            if (self.type and self.type.is_protocol and
+                    self.is_annotated_protocol_member(s) and not self.is_func_scope()):
                 self.fail('All protocol members must have explicitly declared types', s)
             # Set the type if the rvalue is a simple literal (even if the above error occurred).
             if len(s.lvalues) == 1 and isinstance(s.lvalues[0], RefExpr):
@@ -2332,6 +2332,19 @@ def process_type_annotation(self, s: AssignmentStmt) -> None:
             for lvalue in s.lvalues:
                 self.store_declared_types(lvalue, s.type)
 
+    def is_annotated_protocol_member(self, s: AssignmentStmt) -> bool:
+        """Check whether a protocol member is annotated.
+
+        There are some exceptions that can be left unannotated, like ``__slots__``."""
+        return any(
+            (
+                isinstance(lv, NameExpr)
+                and lv.name != '__slots__'
+                and lv.is_inferred_def
+            )
+            for lv in s.lvalues
+        )
+
     def analyze_simple_literal_type(self, rvalue: Expression, is_final: bool) -> Optional[Type]:
         """Return builtins.int if rvalue is an int literal, etc.
 


Test patch:
diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test
--- a/test-data/unit/check-protocols.test
+++ b/test-data/unit/check-protocols.test
@@ -2499,3 +2499,11 @@ reveal_type(abs(3))  # N: Revealed type is 'builtins.int*'
 reveal_type(abs(ALL))  # N: Revealed type is 'builtins.int*'
 [builtins fixtures/float.pyi]
 [typing fixtures/typing-full.pyi]
+
+[case testProtocolWithSlots]
+from typing import Protocol
+
+class A(Protocol):
+    __slots__ = ()
+
+[builtins fixtures/tuple.pyi]
Repository: python/mypy

Issue:
None is not considered Hashable
```python
from typing import Hashable


def f() -> Hashable:
    return None


print(isinstance(None, Hashable))
```
The print returns True, so Python understands that None is hashable, but MyPy complains about incompatible return types:
```
error: Incompatible return value type (got "None", expected "Hashable")
```


Discussion:
Marking as high priority since typeshed is getting new `Hashable` type annotations, and these can cause false positives when updating to a more recent mypy release.

Resolving patch:
diff --git a/mypy/subtypes.py b/mypy/subtypes.py
--- a/mypy/subtypes.py
+++ b/mypy/subtypes.py
@@ -207,10 +207,15 @@ def visit_any(self, left: AnyType) -> bool:
 
     def visit_none_type(self, left: NoneType) -> bool:
         if state.strict_optional:
-            return (isinstance(self.right, NoneType) or
-                    is_named_instance(self.right, 'builtins.object') or
-                    isinstance(self.right, Instance) and self.right.type.is_protocol and
-                    not self.right.type.protocol_members)
+            if isinstance(self.right, NoneType) or is_named_instance(self.right,
+                                                                     'builtins.object'):
+                return True
+            if isinstance(self.right, Instance) and self.right.type.is_protocol:
+                members = self.right.type.protocol_members
+                # None is compatible with Hashable (and other similar protocols). This is
+                # slightly sloppy since we don't check the signature of "__hash__".
+                return not members or members == ["__hash__"]
+            return False
         else:
             return True
 


Test patch:
diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test
--- a/test-data/unit/check-protocols.test
+++ b/test-data/unit/check-protocols.test
@@ -2507,3 +2507,32 @@ class A(Protocol):
     __slots__ = ()
 
 [builtins fixtures/tuple.pyi]
+
+[case testNoneVsProtocol]
+# mypy: strict-optional
+from typing_extensions import Protocol
+
+class MyHashable(Protocol):
+    def __hash__(self) -> int: ...
+
+def f(h: MyHashable) -> None: pass
+f(None)
+
+class Proto(Protocol):
+    def __hash__(self) -> int: ...
+    def method(self) -> None: ...
+
+def g(h: Proto) -> None: pass
+g(None)  # E: Argument 1 to "g" has incompatible type "None"; expected "Proto"
+
+class Proto2(Protocol):
+    def hash(self) -> None: ...
+
+def h(h: Proto2) -> None: pass
+h(None)  # E: Argument 1 to "h" has incompatible type "None"; expected "Proto2"
+
+class EmptyProto(Protocol): ...
+
+def hh(h: EmptyProto) -> None: pass
+hh(None)
+[builtins fixtures/tuple.pyi]
Repository: python/mypy

Issue:
mypy fails with *-stubs is not a valid Python package name
According to PEP 561 [stub-only package](https://www.python.org/dev/peps/pep-0561/#stub-only-packages):

> The name of the stub package MUST follow the scheme foopkg-stubs for type stubs for the package named foopkg. 

However if we invoke mypy on such package providing only path it fails with 

>  *-stubs is not a valid Python package name

This can be reproduced on the [example package](https://github.com/ethanhs/stub-package):

```
git clone https://github.com/ethanhs/stub-package
cd stub-package
mypy ./typedpkg-stubs
typedpkg-stubs is not a valid Python package name
```

Invoking `mypy` with `-p` option works

```
mypy -p typedpkg-stubs
Success: no issues found in 2 source files
```

but the former behavior is rather confusing.

Expected behavior:

- mypy should recognize `*-stubs` paths as roots of stub-only packages.


Tested with:

- mypy 0.761
- mypy-0.770+dev.9101707bd0c96624d09cb31fe573d7e25c89a35c




Discussion:

Resolving patch:
diff --git a/mypy/find_sources.py b/mypy/find_sources.py
--- a/mypy/find_sources.py
+++ b/mypy/find_sources.py
@@ -128,6 +128,8 @@ def crawl_up_dir(self, dir: str) -> Tuple[str, str]:
             base_dir = dir or '.'
         else:
             # Ensure that base is a valid python module name
+            if base.endswith('-stubs'):
+                base = base[:-6]  # PEP-561 stub-only directory
             if not base.isidentifier():
                 raise InvalidSourceList('{} is not a valid Python package name'.format(base))
             parent, base_dir = self.crawl_up_dir(parent_dir)


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
@@ -1176,3 +1176,39 @@ usage: mypy [-h] [-v] [-V] [more options; see below]
             [-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...]
 mypy: error: Invalid error code(s): YOLO
 == Return code: 2
+
+[case testStubsDirectory]
+# cmd: mypy --error-summary pkg-stubs
+[file pkg-stubs/__init__.pyi]
+[file pkg-stubs/thing.pyi]
+class Thing: ...
+[out]
+Success: no issues found in 2 source files
+== Return code: 0
+
+[case testStubsDirectoryFile]
+# cmd: mypy --error-summary pkg-stubs/thing.pyi
+[file pkg-stubs/__init__.pyi]
+[file pkg-stubs/thing.pyi]
+class Thing: ...
+[out]
+Success: no issues found in 1 source file
+== Return code: 0
+
+[case testStubsSubDirectory]
+# cmd: mypy --error-summary src/pkg-stubs
+[file src/pkg-stubs/__init__.pyi]
+[file src/pkg-stubs/thing.pyi]
+class Thing: ...
+[out]
+Success: no issues found in 2 source files
+== Return code: 0
+
+[case testStubsSubDirectoryFile]
+# cmd: mypy --error-summary src/pkg-stubs/thing.pyi
+[file src/pkg-stubs/__init__.pyi]
+[file src/pkg-stubs/thing.pyi]
+class Thing: ...
+[out]
+Success: no issues found in 1 source file
+== Return code: 0
Repository: python/mypy

Issue:
Overload with Dict only checks overlap on keys
Using mypy 0.782, it looks like overlap is only checked on dictionary keys.

```
from typing import Dict, overload

class A: pass
class B: pass

@overload
def foo(x: Dict[A, A]) -> A: ...

@overload
def foo(x: Dict[A, B]) -> B: ...

def foo(x):
    for k, v in x: return v
    raise ValueError
```

gives `error: Overloaded function signatures 1 and 2 overlap with incompatible return types`. I get the same error when using `Mapping` instead of `Dict.


Discussion:
It's not specific to `Dict` or `Mapping`, it seems to happen with any generic class over two type variables.

I do not actually have a good theory as to why it happens, but I suspect there is a case where a type can be either a `Dict[A, A]` or a `Dict[A, B]`. Perhaps the possibility of multiple inheritance is key:
```
class C(A, B): ...
a: Dict[A, C] {}
foo(a)
```
Thanks for the response. If I change the 2nd overload to `Dict[B, B]`, then mypy doesn't give any errors. I think the ambiguity with multiple inheritance still exists in this case (e.g. `Dict[C, C]`), yes?
I suspect it's this code: https://github.com/python/mypy/blob/master/mypy/meet.py#L342. `is_overlapping_types()` returns True if any of the type arguments are overlapping, but presumably it should only do that if they all overlap.

Resolving patch:
diff --git a/mypy/meet.py b/mypy/meet.py
--- a/mypy/meet.py
+++ b/mypy/meet.py
@@ -339,9 +339,9 @@ def _type_object_overlap(left: Type, right: Type) -> bool:
             # Or, to use a more concrete example, List[Union[A, B]] and List[Union[B, C]]
             # would be considered partially overlapping since it's possible for both lists
             # to contain only instances of B at runtime.
-            for left_arg, right_arg in zip(left.args, right.args):
-                if _is_overlapping_types(left_arg, right_arg):
-                    return True
+            if all(_is_overlapping_types(left_arg, right_arg)
+                   for left_arg, right_arg in zip(left.args, right.args)):
+                return True
 
         return False
 


Test patch:
diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test
--- a/test-data/unit/check-overloading.test
+++ b/test-data/unit/check-overloading.test
@@ -5151,3 +5151,25 @@ compose(ID, fakeint)("test")
 reveal_type(compose(ID, fakeint))  # N: Revealed type is 'def (Union[builtins.str, builtins.bytes]) -> __main__.ID*'
 
 [builtins fixtures/tuple.pyi]
+
+[case testOverloadTwoTypeArgs]
+from typing import Generic, overload, TypeVar, Any
+
+T1 = TypeVar("T1")
+T2 = TypeVar("T2")
+
+class A: ...
+class B: ...
+class G(Generic[T1, T2]): ...
+
+@overload
+def f1(g: G[A, A]) -> A: ...
+@overload
+def f1(g: G[A, B]) -> B: ...
+def f1(g: Any) -> Any: ...
+
+@overload
+def f2(g: G[A, Any]) -> A: ...  # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
+@overload
+def f2(g: G[A, B], x: int = ...) -> B: ...
+def f2(g: Any, x: int = ...) -> Any: ...
Repository: python/mypy

Issue:
mypy doesn't complain about TypeVars used as values in expressions
The following code currently type checks:
```
from typing import TypeVar
T = TypeVar("T")
x = 5
x + T
T()
def f(x): ...
f(T)
```


Discussion:
it's handy to pass TypeVars into some functions, e.g. `cast(T, foo)`
Currently the type of a type variable is `Any`. It could be better to make the type `object`, so that it could still be used as a value (for specialized use cases), but most invalid uses would be flagged.

`cast` is special cased so it won't be an issue in any case.

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -228,6 +228,8 @@ def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
             result = self.alias_type_in_runtime_context(node, node.no_args, e,
                                                         alias_definition=e.is_alias_rvalue
                                                         or lvalue)
+        elif isinstance(node, (TypeVarExpr, ParamSpecExpr)):
+            result = self.object_type()
         else:
             if isinstance(node, PlaceholderNode):
                 assert False, 'PlaceholderNode %r leaked to checker' % node.fullname


Test patch:
diff --git a/test-data/unit/check-class-namedtuple.test b/test-data/unit/check-class-namedtuple.test
--- a/test-data/unit/check-class-namedtuple.test
+++ b/test-data/unit/check-class-namedtuple.test
@@ -582,7 +582,8 @@ class Base(NamedTuple):
         reveal_type(self.x)  # N: Revealed type is 'builtins.int'
         self.x = 3  # E: Property "x" defined in "Base" is read-only
         self[1]  # E: Tuple index out of range
-        reveal_type(self[T])  # N: Revealed type is 'builtins.int'
+        reveal_type(self[T])  # N: Revealed type is 'Any' \
+                              # E: Invalid tuple index type (actual type "object", expected type "Union[int, slice]")
         return self.x
     def bad_override(self) -> int:
         return self.x
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
@@ -2803,3 +2803,10 @@ def f() -> int:  # E: Missing return statement
     x: int
     assert isinstance(x, int), '...'
 [builtins fixtures/isinstance.pyi]
+
+[case testTypeVarAsValue]
+from typing import TypeVar
+T = TypeVar("T")
+x: int
+x + T  # E: Unsupported operand types for + ("int" and "object")
+T()  # E: "object" not callable
Repository: python/mypy

Issue:
Type sometimes not inferred for argument when using --disallow-untyped-calls
The `reveal_type` in the example below produces no output, since the expression is not type checked:

```py
# mypy: disallow-untyped-calls

def f(x):
    pass

y = 1
f(reveal_type(y))  # No output!
```

I think that this may also result in crashes, at least when calculating code stats.


Discussion:

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -317,7 +317,7 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
                 self.chk.in_checked_function() and
                 isinstance(callee_type, CallableType)
                 and callee_type.implicit):
-            return self.msg.untyped_function_call(callee_type, e)
+            self.msg.untyped_function_call(callee_type, e)
         # Figure out the full name of the callee for plugin lookup.
         object_type = None
         member = None


Test patch:
diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test
--- a/test-data/unit/check-flags.test
+++ b/test-data/unit/check-flags.test
@@ -1587,3 +1587,12 @@ def bad_return_type() -> str:
     return None  # E: Incompatible return value type (got "None", expected "str")  [return-value]
 
 bad_return_type('no args taken!')
+
+[case testDisallowUntypedCallsArgType]
+# flags: --disallow-untyped-calls
+def f(x):
+    pass
+
+y = 1
+f(reveal_type(y)) # E: Call to untyped function "f" in typed context \
+                  # N: Revealed type is 'builtins.int'
Repository: python/mypy

Issue:
removed analysis.py is still used in ./mypyc/transform/uninit.py
seen when trying to generate manual pages with 0.790 and python3.9:

I: pybuild base:217: /usr/bin/python3.9 setup.py build 
Traceback (most recent call last):
  File "/packages/tmp/mypy-0.790/setup.py", line 144, in <module>
    from mypyc.build import mypycify
  File "/packages/tmp/mypy-0.790/mypyc/build.py", line 43, in <module>
    from mypyc.codegen import emitmodule
  File "/packages/tmp/mypy-0.790/mypyc/codegen/emitmodule.py", line 41, in <module>
    from mypyc.transform.uninit import insert_uninit_checks
  File "/packages/tmp/mypy-0.790/mypyc/transform/uninit.py", line 5, in <module>
    from mypyc.analysis.dataflow import (
ModuleNotFoundError: No module named 'mypyc.analysis'

apparently the analysis module was removed, however mypyc/transform/uninit.py still uses it


Discussion:
https://github.com/python/mypy/tree/master/mypyc/analysis still exists
I can't repro with `python3.9 setup.py build` on master, so closing. Feel free to reopen with more details :-)

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -181,7 +181,7 @@ def run(self):
       packages=[
           'mypy', 'mypy.test', 'mypy.server', 'mypy.plugins', 'mypy.dmypy',
           'mypyc', 'mypyc.test', 'mypyc.codegen', 'mypyc.ir', 'mypyc.irbuild',
-          'mypyc.primitives', 'mypyc.transform'
+          'mypyc.primitives', 'mypyc.transform', 'mypyc.analysis'
       ],
       package_data={'mypy': package_data},
       scripts=['scripts/mypyc'],


Test patch:
Repository: python/mypy

Issue:
Path escaping bug in HTML report
Bug in MyPy 0.780 with Python 3.7.1 on Windows 10, including args `--html_report coverage_report/`

The template for the HTML report includes a path without normalizing the path separators, so on windows uses '\\' instead of '/', and this appears in the browser as %5C: 

    <a href="html/example%5Capp%5Cmain.py.html">example.app.main</a>

The browser then misbehaves when when navigating to this url with backslashes in it, appearing like so in the URL bar

    file:///C:/proj/example/coverage_report/html/example\app\main.py.html

It can be found [here](https://github.com/python/mypy/blob/793cf187f0e529f9d6623a512ae0149124ded542/mypy/xml/mypy-html.xslt#L40) in the template, and I believe is formatting [the name property](https://github.com/python/mypy/blob/793cf187f0e529f9d6623a512ae0149124ded542/mypy/nodes.py#L286-L288) of the `MyPyFile` class




Discussion:
Thanks for reporting! PRs are welcome.

Resolving patch:
diff --git a/mypy/report.py b/mypy/report.py
--- a/mypy/report.py
+++ b/mypy/report.py
@@ -522,7 +522,7 @@ def on_finish(self) -> None:
             etree.SubElement(root, 'file',
                              file_info.attrib(),
                              module=file_info.module,
-                             name=file_info.name,
+                             name=pathname2url(file_info.name),
                              total=str(file_info.total()))
         xslt_path = os.path.relpath('mypy-html.xslt', '.')
         transform_pi = etree.ProcessingInstruction('xml-stylesheet',


Test patch:
Repository: python/mypy

Issue:
Python 2.7 stub for subprocess



Discussion:
done


Resolving patch:
diff --git a/mypyc/codegen/emitclass.py b/mypyc/codegen/emitclass.py
--- a/mypyc/codegen/emitclass.py
+++ b/mypyc/codegen/emitclass.py
@@ -627,8 +627,11 @@ def generate_dealloc_for_class(cl: ClassIR,
     emitter.emit_line('{}({} *self)'.format(dealloc_func_name, cl.struct_name(emitter.names)))
     emitter.emit_line('{')
     emitter.emit_line('PyObject_GC_UnTrack(self);')
+    # The trashcan is needed to handle deep recursive deallocations
+    emitter.emit_line('CPy_TRASHCAN_BEGIN(self, {})'.format(dealloc_func_name))
     emitter.emit_line('{}(self);'.format(clear_func_name))
     emitter.emit_line('Py_TYPE(self)->tp_free((PyObject *)self);')
+    emitter.emit_line('CPy_TRASHCAN_END(self)')
     emitter.emit_line('}')
 
 


Test patch:
Repository: python/mypy

Issue:
Make `--explicit-package-bases` revertible
**Feature**

`--explicit-package-bases` should have an inverse: `--no-explicit-package-bases`.

**Pitch**

I have a `mypy.ini` that declares `explicit_package_bases = True`. It would be convenient to be able to override this setting from the command line by passing  `--no-explicit-package-bases`.

My use case is that I have a mono repo with a shared `mypy.ini`, where I mostly want to use `--explicit-package-bases`.
However, in some sub directories I have the following structure:
```
sub-dir
│
└───src
│   └───pkg
│   │   └─── # Package content
│   └───tests
│
└───noxfile.py
```
The `noxfile.py` is used to trigger a `mypy src/ tests/ --config {PATH_TO_CONFIG}` session with `sub-dir` as working directory.
I would like to use the same `mypy.ini` I use in the rest of the repo to not have to duplicate settings, but this sets `--explicit-package-bases`. Unfortunately this leads to `mypy` thinking that `src` is the namespace of `pkg`, since it is located at the working directory. The most convenient solution for me would be to pass `--no-explict-package-bases` when invoking `mypy` in `sub-dir`.


Discussion:
I might be misunderstanding, but I'm not sure that would help if `pkg` is actually a namespace package, since mypy wouldn't correctly map files to module names. Try putting src in your mypy path instead? (on the lines of the example in point 3 of https://mypy.readthedocs.io/en/latest/running_mypy.html#mapping-file-paths-to-modules)
Sorry, maybe the example was not clear enough.

`pkg` is not a namespace package, but when mypy is invoked with the working directory `sub-dir`, it thinks that `pkg` is a namespace package. I would like to be able to explicitly turn of `explicit-namespace-packages` when I run `mypy` in `sub-dir`.

Resolving patch:
diff --git a/mypy/main.py b/mypy/main.py
--- a/mypy/main.py
+++ b/mypy/main.py
@@ -805,9 +805,10 @@ def add_invertible_flag(flag: str,
         title="Running code",
         description="Specify the code you want to type check. For more details, see "
                     "mypy.readthedocs.io/en/stable/running_mypy.html#running-mypy")
-    code_group.add_argument(
-        '--explicit-package-bases', action='store_true',
-        help="Use current directory and MYPYPATH to determine module names of files passed")
+    add_invertible_flag(
+        '--explicit-package-bases', default=False,
+        help="Use current directory and MYPYPATH to determine module names of files passed",
+        group=code_group)
     code_group.add_argument(
         "--exclude",
         metavar="PATTERN",


Test patch: