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

Issue:
Feature: Do expansion on all path options in configuration
Previously issues #6508 and #7259 added home directory and environment variable expansion to the `cache_dir` option in the configuration file. It might make sense to the the same for all the other options also referring to paths, these would include: `files`, `mypy_path`, `custom_typeshed_dir` and `python_executable`.

Some review may be necessary to determine if this is not appropriate for any of these options.


Discussion:

Resolving patch:
diff --git a/mypy/config_parser.py b/mypy/config_parser.py
--- a/mypy/config_parser.py
+++ b/mypy/config_parser.py
@@ -34,6 +34,14 @@ def parse_version(v: str) -> Tuple[int, int]:
     return major, minor
 
 
+def expand_path(path: str) -> str:
+    """Expand the user home directory and any environment variables contained within
+    the provided path.
+    """
+
+    return os.path.expandvars(os.path.expanduser(path))
+
+
 def split_and_match_files(paths: str) -> List[str]:
     """Take a string representing a list of files/directories (with support for globbing
     through the glob library).
@@ -45,7 +53,7 @@ def split_and_match_files(paths: str) -> List[str]:
     expanded_paths = []
 
     for path in paths.split(','):
-        path = path.strip()
+        path = expand_path(path.strip())
         globbed_files = fileglob.glob(path, recursive=True)
         if globbed_files:
             expanded_paths.extend(globbed_files)
@@ -63,8 +71,8 @@ def split_and_match_files(paths: str) -> List[str]:
     'python_version': parse_version,
     'strict_optional_whitelist': lambda s: s.split(),
     'custom_typing_module': str,
-    'custom_typeshed_dir': str,
-    'mypy_path': lambda s: [p.strip() for p in re.split('[,:]', s)],
+    'custom_typeshed_dir': expand_path,
+    'mypy_path': lambda s: [expand_path(p.strip()) for p in re.split('[,:]', s)],
     'files': split_and_match_files,
     'quickstart_file': str,
     'junit_xml': str,
@@ -75,6 +83,8 @@ def split_and_match_files(paths: str) -> List[str]:
     'always_true': lambda s: [p.strip() for p in s.split(',')],
     'always_false': lambda s: [p.strip() for p in s.split(',')],
     'package_root': lambda s: [p.strip() for p in s.split(',')],
+    'cache_dir': expand_path,
+    'python_executable': expand_path,
 }  # type: Final
 
 
@@ -223,8 +233,6 @@ def parse_section(prefix: str, template: Options,
         except ValueError as err:
             print("%s%s: %s" % (prefix, key, err), file=stderr)
             continue
-        if key == 'cache_dir':
-            v = os.path.expandvars(os.path.expanduser(v))
         if key == 'silent_imports':
             print("%ssilent_imports has been replaced by "
                   "ignore_missing_imports=True; follow_imports=skip" % prefix, file=stderr)


Test patch:
Repository: python/mypy

Issue:
crash interpolating bytes with dictionary
```
$ cat example.py
print(b'%(x)s' % {b'x': b'data'})
$ python3 example.py
b'data'
$ mypy example.py --show-traceback
mypy example.py  --show-traceback
example.py:1: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.720
Traceback (most recent call last):
  File "mypy/checkexpr.py", line 3404, in accept
  File "mypy/nodes.py", line 1708, in accept
  File "mypy/checkexpr.py", line 1891, in visit_op_expr__ExpressionVisitor_glue
  File "mypy/checkexpr.py", line 1906, in visit_op_expr
  File "mypy/checkstrformat.py", line 97, in check_str_interpolation
  File "mypy/checkstrformat.py", line 178, in check_mapping_str_interpolation
TypeError: StrExpr object expected
example.py:1: : note: use --pdb to drop into pdb
```



Discussion:

Resolving patch:
diff --git a/mypy/checkstrformat.py b/mypy/checkstrformat.py
--- a/mypy/checkstrformat.py
+++ b/mypy/checkstrformat.py
@@ -174,11 +174,11 @@ def check_mapping_str_interpolation(self, specifiers: List[ConversionSpecifier],
                                         replacements: Expression,
                                         expr: FormatStringExpr) -> None:
         if (isinstance(replacements, DictExpr) and
-                all(isinstance(k, (StrExpr, BytesExpr))
+                all(isinstance(k, (StrExpr, BytesExpr, UnicodeExpr))
                     for k, v in replacements.items)):
             mapping = {}  # type: Dict[str, Type]
             for k, v in replacements.items:
-                key_str = cast(StrExpr, k).value
+                key_str = cast(FormatStringExpr, k).value
                 mapping[key_str] = self.accept(v)
 
             for specifier in specifiers:


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
@@ -1223,6 +1223,7 @@ b'%a' % 3
 [case testStringInterpolationMappingTypes]
 '%(a)d %(b)s' % {'a': 1, 'b': 's'}
 '%(a)d %(b)s' % {'a': 's', 'b': 1}  # E: Incompatible types in string interpolation (expression has type "str", placeholder with key 'a' has type "Union[int, float]")
+b'%(x)s' % {b'x': b'data'}
 [builtins fixtures/primitives.pyi]
 
 [case testStringInterpolationMappingKeys]
Repository: python/mypy

Issue:
super().__init_subclass__() fails
```
$ mypy bin/insubcl.py 
bin/insubcl.py:3: error: Too few arguments for "__init_subclass__" of "object"
$ cat bin/insubcl.py 
class X:
    def __init_subclass__(cls) -> None:
        super().__init_subclass__()
$ mypy --version
mypy 0.730+dev.36fcf1d5aa0f5656ac367bc66aba6e390f82ea9a.dirty
```

This code works correctly at runtime and the latest release accepts it.


Discussion:
I didn't check, but it is likely a regression caused by https://github.com/python/mypy/pull/7232

cc @tavianator 

Resolving patch:
diff --git a/mypy/nodes.py b/mypy/nodes.py
--- a/mypy/nodes.py
+++ b/mypy/nodes.py
@@ -439,7 +439,7 @@ class FuncBase(Node):
                  'unanalyzed_type',
                  'info',
                  'is_property',
-                 'is_class',        # Uses "@classmethod"
+                 'is_class',        # Uses "@classmethod" (explicit or implicit)
                  'is_static',       # Uses "@staticmethod"
                  'is_final',        # Uses "@final"
                  '_fullname',
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -611,10 +611,12 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
             if not func.arguments:
                 self.fail('Method must have at least one argument', func)
             elif isinstance(functype, CallableType):
+                if func.name() == '__init_subclass__':
+                    func.is_class = True
                 self_type = functype.arg_types[0]
                 if isinstance(self_type, AnyType):
                     leading_type = fill_typevars(info)  # type: Type
-                    if func.is_class or func.name() in ('__new__', '__init_subclass__'):
+                    if func.is_class or func.name() == '__new__':
                         leading_type = self.class_type(leading_type)
                     func.type = replace_implicit_first_type(functype, leading_type)
 


Test patch:
diff --git a/test-data/unit/check-super.test b/test-data/unit/check-super.test
--- a/test-data/unit/check-super.test
+++ b/test-data/unit/check-super.test
@@ -252,6 +252,12 @@ def g(b: B) -> None:
     super(B, b).f('42')
     super(B, b).f(42) # E: Argument 1 to "f" of "A" has incompatible type "int"; expected "str"
 
+[case testSuperInInitSubclass]
+class A:
+    def __init_subclass__(cls) -> None:
+        super().__init_subclass__()
+[builtins fixtures/__init_subclass__.pyi]
+
 
 -- Invalid uses of super()
 -- -----------------------
Repository: python/mypy

Issue:
Erroneous 'Too many arguments' error
It appears as though mypy treats argument unpacking as containing at least one element. Here's an example:

```python
from typing import List

class Foo:
    pass

args: List[str] = []
packet = Foo(*args)
```

```
tbug.py:7: error: Too many arguments for "Foo"
```

I guess Mypy is assuming that you would never unpack zero arguments.

In my particular case I have a registry of 'packet classes' and a dynamic number of arguments parsed from the network. Some of those packet classes legitimately have 0 parameters, and Mypy gives me an error about each of them.

Running `mypy 0.600` with no other flags.



Discussion:

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -1233,7 +1233,9 @@ def check_for_extra_actual_arguments(self,
         for i, kind in enumerate(actual_kinds):
             if i not in all_actuals and (
                     kind != nodes.ARG_STAR or
-                    not is_empty_tuple(actual_types[i])):
+                    # We accept the other iterables than tuple (including Any)
+                    # as star arguments because they could be empty, resulting no arguments.
+                    is_non_empty_tuple(actual_types[i])):
                 # Extra actual: not matched by a formal argument.
                 ok = False
                 if kind != nodes.ARG_NAMED:
@@ -3833,9 +3835,9 @@ def is_async_def(t: Type) -> bool:
     return isinstance(t, Instance) and t.type.fullname() == 'typing.Coroutine'
 
 
-def is_empty_tuple(t: Type) -> bool:
+def is_non_empty_tuple(t: Type) -> bool:
     t = get_proper_type(t)
-    return isinstance(t, TupleType) and not t.items
+    return isinstance(t, TupleType) and bool(t.items)
 
 
 def is_duplicate_mapping(mapping: List[int], actual_kinds: List[int]) -> bool:


Test patch:
diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test
--- a/test-data/unit/check-varargs.test
+++ b/test-data/unit/check-varargs.test
@@ -353,7 +353,7 @@ class CC(C): pass
 from typing import Any
 d, a = None, None # type: (Any, A)
 f(a, a, *d) # Fail
-f(a, *d)    # Fail
+f(a, *d)    # Ok
 f(*d)       # Ok
 
 g(*d)
@@ -366,7 +366,6 @@ class A: pass
 [builtins fixtures/list.pyi]
 [out]
 main:3: error: Too many arguments for "f"
-main:4: error: Too many arguments for "f"
 
 [case testListVarArgsAndSubtyping]
 from typing import List
@@ -448,6 +447,21 @@ def f(x: int, *, y: str) -> None: pass
 f(y='x', *(1,))
 [builtins fixtures/list.pyi]
 
+[case testVarArgsEmptyList]
+from typing import List
+
+def foo() -> None:
+    pass
+
+lst: List[int] = []
+foo(*lst)
+[builtins fixtures/list.pyi]
+
+[case testVarArgsEmptyTuple]
+def foo() -> None:
+    pass
+
+foo(*())
 
 -- Overloads + varargs
 -- -------------------
Repository: python/mypy

Issue:
Using a class inherited from tuple as a bound in TypeVar
I think I've encountered mypy bug. Here's the code I'm trying to check with mypy (sorry if it's big, I think additional code helps debugging):
```python
from typing import NamedTuple, Type, TypeVar

# case 1 - inheriting from custom class, mypy check passes.
T1 = TypeVar("T1", bound="B")

class A:
    pass

class B(A):
    @classmethod
    def method(cls) -> None:
        print("B class method")

    @classmethod
    def factory(cls: Type[T1]) -> T1:
        cls.method()
        return cls()

# case 2 - inheriting from named tuple, mypy check fails.
T2 = TypeVar("T2", bound="C")

class C(NamedTuple):
    @classmethod
    def method(cls) -> None:
        print("C class method")

    @classmethod
    def factory(cls: Type[T2]) -> T2:
        cls.method()  # error: "Type[T2]" has no attribute "method"
        return cls()
```

* What is the actual behavior/output?
```bash
$ mypy test.py
test.py:29: error: "Type[T2]" has no attribute "method"
```
* What is the behavior/output you expect?
  **no errors**
* What are the versions of mypy and Python you are using?
  **python 3.7.3, mypy both `0.720` and `0.730+dev.811c201551ecb151f83e982da3ad8f46fa2ebe84`**
* What are the mypy flags you are using?
  **no flags**

I've been able to track the error till [this line](https://github.com/python/mypy/blob/811c201551ecb151f83e982da3ad8f46fa2ebe84/mypy/checkmember.py#L261) `mypy/checkmember.py#L261`. In first case `upper_bound` is `test.B`, but in second it's `Tuple[, fallback=test.C]` and fallback is not checked. I'm not sure how to proceed from here though because it's probably not safe to always check a fallback.


Discussion:
Yes, this is a bug. The simplest fix would be to add an `isinstance(upper_bound, TupleType)` check and update `item` to point to the fallback. Do you want to give it a try?

Resolving patch:
diff --git a/mypy/checkmember.py b/mypy/checkmember.py
--- a/mypy/checkmember.py
+++ b/mypy/checkmember.py
@@ -261,6 +261,8 @@ def analyze_type_type_member_access(name: str,
         upper_bound = get_proper_type(typ.item.upper_bound)
         if isinstance(upper_bound, Instance):
             item = upper_bound
+        elif isinstance(upper_bound, TupleType):
+            item = tuple_fallback(upper_bound)
     elif isinstance(typ.item, TupleType):
         item = tuple_fallback(typ.item)
     elif isinstance(typ.item, FunctionLike) and typ.item.is_type_obj():


Test patch:
diff --git a/test-data/unit/check-bound.test b/test-data/unit/check-bound.test
--- a/test-data/unit/check-bound.test
+++ b/test-data/unit/check-bound.test
@@ -169,6 +169,19 @@ def f(x: T) -> int:
 [builtins fixtures/classmethod.pyi]
 
 
+[case testBoundClassMethodWithNamedTupleBase]
+from typing import NamedTuple, Type, TypeVar
+class A(NamedTuple):
+    @classmethod
+    def foo(cls) -> None: ...
+
+T = TypeVar('T', bound=A)
+def f(x: Type[T]) -> None:
+    reveal_type(x.foo)  # N: Revealed type is 'def ()'
+    x.foo()
+[builtins fixtures/classmethod.pyi]
+
+
 [case testBoundStaticMethod]
 from typing import TypeVar
 class A0:
Repository: python/mypy

Issue:
Protocols are not recognised as such if declared together with Generic[T]
This test case:
```
[case testRuntimeProtoTwoBases]
from typing_extensions import Protocol, runtime_checkable
from typing import TypeVar, Generic

T = TypeVar('T')

@runtime_checkable
class P(Protocol, Generic[T]):
    attr: T

class C:
    attr: int

x: P[int] = C()
```
fails with
```
main:7: error: @runtime_checkable can only be used with protocol classes
main:13: error: Incompatible types in assignment (expression has type "C", variable has type "P[int]")
```
Both replacing `Protocol, Generic[T]` with `Protocol[T]`, and changing the order of bases to `Generic[T], Protocol` fixes the issue.


Discussion:

Resolving patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -1227,7 +1227,8 @@ class Foo(Bar, Generic[T]): ...
                 if declared_tvars:
                     self.fail('Only single Generic[...] or Protocol[...] can be in bases', context)
                 removed.append(i)
-                tvars, is_protocol = result
+                tvars = result[0]
+                is_protocol |= result[1]
                 declared_tvars.extend(tvars)
             if isinstance(base, UnboundType):
                 sym = self.lookup_qualified(base.name, base)


Test patch:
diff --git a/test-data/unit/check-semanal-error.test b/test-data/unit/check-semanal-error.test
--- a/test-data/unit/check-semanal-error.test
+++ b/test-data/unit/check-semanal-error.test
@@ -97,3 +97,19 @@ def m() -> None: ...  # E: Name 'm' already defined (possibly by an import)
 def f() -> None: ...  # E: Name 'f' already defined (possibly by an import)
 
 [out]
+
+[case testRuntimeProtoTwoBases]
+from typing_extensions import Protocol, runtime_checkable
+from typing import TypeVar, Generic
+
+T = TypeVar('T')
+
+@runtime_checkable
+class P(Protocol, Generic[T]):
+    attr: T
+
+class C:
+    attr: int
+
+x: P[int] = C()
+[out]
Repository: python/mypy

Issue:
`new_semantic_analyzer = ...` raises AttributeError
Having a setting for `new_semantic_analyzer` in setup.cfg raises an AttributeError for me on master (`mypy 0.730+dev.475148f2b08dbe0126fc3e22c277132b65e47007`). This should probably fall back to the same error message as other unknown options yields :) 

```
Traceback (most recent call last):
  File "/home/anton/.pyenv/versions/fleet-manager-service/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "/home/anton/.pyenv/versions/3.7.0/envs/fleet-manager-service/lib/python3.7/site-packages/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/anton/.pyenv/versions/3.7.0/envs/fleet-manager-service/lib/python3.7/site-packages/mypy/main.py", line 63, in main
    fscache=fscache)
  File "/home/anton/.pyenv/versions/3.7.0/envs/fleet-manager-service/lib/python3.7/site-packages/mypy/main.py", line 709, in process_options
    parse_config_file(options, config_file, stdout, stderr)
  File "/home/anton/.pyenv/versions/3.7.0/envs/fleet-manager-service/lib/python3.7/site-packages/mypy/config_parser.py", line 132, in parse_config_file
    setattr(options, k, v)
AttributeError: can't set attribute
```


Discussion:
This is due to `Options.new_semantic_analyzer` being kept and converted into a property. I will submit a PR to fix this.

Resolving patch:
diff --git a/mypy/config_parser.py b/mypy/config_parser.py
--- a/mypy/config_parser.py
+++ b/mypy/config_parser.py
@@ -178,7 +178,12 @@ def parse_section(prefix: str, template: Options,
         if key in config_types:
             ct = config_types[key]
         else:
-            dv = getattr(template, key, None)
+            dv = None
+            # We have to keep new_semantic_analyzer in Options
+            # for plugin compatibility but it is not a valid option anymore.
+            assert hasattr(template, 'new_semantic_analyzer')
+            if key != 'new_semantic_analyzer':
+                dv = getattr(template, key, None)
             if dv is None:
                 if key.endswith('_report'):
                     report_type = key[:-7].replace('_', '-')


Test patch:
Repository: python/mypy

Issue:
daemon crashes that `report_internal_error` don't notify client
Daemon crashes that manifest as exceptions reaching the server code have a traceback sent to the client, but errors that are caught somewhere and reported to `report_internal_error` just print the error there and exit.

We should arrange for the crash message to get sent to the client in this case as well.


Discussion:

Resolving patch:
diff --git a/mypy/dmypy_server.py b/mypy/dmypy_server.py
--- a/mypy/dmypy_server.py
+++ b/mypy/dmypy_server.py
@@ -175,6 +175,7 @@ def __init__(self, options: Options,
 
         self.fscache = FileSystemCache()
 
+        options.raise_exceptions = True
         options.incremental = True
         options.fine_grained_incremental = True
         options.show_traceback = True


Test patch:
Repository: python/mypy

Issue:
Mypy not treating bytes as typing.ByteString
According to the [docs](https://docs.python.org/3/library/typing.html#typing.ByteString), an argument typed as `bytes` should also accept `bytearray` and `memoryview`, but this doesn't seem to be the case.

The following example demonstrates this:

```python
def process(b: bytes) -> None:
    pass

process(memoryview(b"foo"))
```

Mypy produces the following error:

```
error: Argument 1 to "process" has incompatible type "memoryview"; expected "bytes"
```

I found https://github.com/python/mypy/issues/4871 which is essentially the same issue. If there hasn't been any relevant changes since April 2008, perhaps its a docs issue only?


Discussion:
In typeshed I have directed people to just use `bytes` instead of a union with `memoryview`, because of the documentation. In case we just change the documentation, we'd need to change the stubs as well.
I think this should be just fixed in mypy. It should be not hard, one just needs to update `TYPE_PROMOTIONS_PYTHON2` and `TYPE_PROMOTIONS_PYTHON3` in `mypy/semanal_classprop.py` and add a test for this. Anyone wants to make a PR?

Resolving patch:
diff --git a/mypy/semanal_classprop.py b/mypy/semanal_classprop.py
--- a/mypy/semanal_classprop.py
+++ b/mypy/semanal_classprop.py
@@ -30,6 +30,7 @@
 TYPE_PROMOTIONS_PYTHON3 = TYPE_PROMOTIONS.copy()  # type: Final
 TYPE_PROMOTIONS_PYTHON3.update({
     'builtins.bytearray': 'bytes',
+    'builtins.memoryview': 'bytes',
 })
 
 # Hard coded type promotions for Python 2.
@@ -41,6 +42,7 @@
 TYPE_PROMOTIONS_PYTHON2.update({
     'builtins.str': 'unicode',
     'builtins.bytearray': 'str',
+    'builtins.memoryview': 'str',
 })
 
 


Test patch:
diff --git a/test-data/unit/check-type-promotion.test b/test-data/unit/check-type-promotion.test
--- a/test-data/unit/check-type-promotion.test
+++ b/test-data/unit/check-type-promotion.test
@@ -26,6 +26,11 @@ def f(x: bytes) -> None: pass
 f(bytearray(b''))
 [builtins fixtures/primitives.pyi]
 
+[case testPromoteMemoryviewToBytes]
+def f(x: bytes) -> None: pass
+f(memoryview(b''))
+[builtins fixtures/primitives.pyi]
+
 [case testNarrowingDownFromPromoteTargetType]
 y = 0.0
 y = 1
Repository: python/mypy

Issue:
False positive with --warn-unreachable: TypedDict may be empty
Hi,

I believe there is a false positive related to empty `TypedDict`s with `total=False`.

Using `--warn-unreachable`:

```python
from typing_extensions import TypedDict

class A(TypedDict, total=False):
    ...

class B(TypedDict, total=False):
    x: int

a: A = {}
b: B = {}

if not a:
    print('a is empty')

if not b:
    print('b is empty')  # error: Statement is unreachable

```

Using Python 3.7.4, tested with both mypy 0.740 and mypy master.



Discussion:
Thanks for reporting! This looks like something easy to fix. In the `TypedDictType` constructor in `mypy/types.py` one just needs to use `required_keys` instead of `items` to determine value of `self.can_be_false`.

Resolving patch:
diff --git a/mypy/types.py b/mypy/types.py
--- a/mypy/types.py
+++ b/mypy/types.py
@@ -1376,7 +1376,7 @@ def __init__(self, items: 'OrderedDict[str, Type]', required_keys: Set[str],
         self.required_keys = required_keys
         self.fallback = fallback
         self.can_be_true = len(self.items) > 0
-        self.can_be_false = len(self.items) == 0
+        self.can_be_false = len(self.required_keys) == 0
 
     def accept(self, visitor: 'TypeVisitor[T]') -> T:
         return visitor.visit_typeddict_type(self)


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
@@ -1126,6 +1126,25 @@ ll = [b, c]
 f(ll) # E: Argument 1 to "f" has incompatible type "List[TypedDict({'x'?: int, 'z'?: str})]"; expected "A"
 [builtins fixtures/dict.pyi]
 
+[case testNonTotalTypedDictCanBeEmpty]
+# flags: --warn-unreachable
+from mypy_extensions import TypedDict
+
+class A(TypedDict):
+    ...
+
+class B(TypedDict, total=False):
+    x: int
+
+a: A = {}
+b: B = {}
+
+if not a:
+    reveal_type(a) # N: Revealed type is 'TypedDict('__main__.A', {})'
+
+if not b:
+    reveal_type(b) # N: Revealed type is 'TypedDict('__main__.B', {'x'?: builtins.int})'
+[builtins fixtures/dict.pyi]
 
 -- Create Type (Errors)