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

Issue:
Inconsistent behavior of `# type: ignore` for whole file between python versions
* **_Are you reporting a bug, or opening a feature request?_**

A bug


* **_Please insert below the code you are checking with mypy_**

`test.py`:
```python
# type: ignore

def test1():
    return 1

print(test1())
```


* **_What is the actual output?_**

with `mypy test.py --strict`:
```
Success: no issues found in 1 source file
```

with `mypy test.py --strict -2`:
```
test.py:1: error: unused 'type: ignore' comment
test.py:3: error: Function is missing a return type annotation
test.py:6: error: Call to untyped function "test1" in typed context
Found 3 errors in 1 file (checked 1 source file)
```


* **_What is the output you expect?_**

both `mypy test.py --strict` and `mypy test.py --strict -2`:
```
Success: no issues found in 1 source file
```


* **_What are the versions of mypy and Python you are using?_**

  * mypy: 0.740
  * python: 3.7.4


* **_Do you see the same issue after installing mypy from Git master?_**

Yes (on 09c1fc7)


Discussion:
I see the issue here. I'll have a patch up soon.

Nice catch @isac322!

Resolving patch:
diff --git a/mypy/fastparse2.py b/mypy/fastparse2.py
--- a/mypy/fastparse2.py
+++ b/mypy/fastparse2.py
@@ -349,7 +349,7 @@ def visit_Module(self, mod: ast27.Module) -> MypyFile:
                 self.type_ignores[ti.lineno] = parsed
             else:
                 self.fail(INVALID_TYPE_IGNORE, ti.lineno, -1)
-        body = self.fix_function_overloads(self.translate_stmt_list(mod.body))
+        body = self.fix_function_overloads(self.translate_stmt_list(mod.body, module=True))
         return MypyFile(body,
                         self.imports,
                         False,


Test patch:
diff --git a/test-data/unit/check-ignore.test b/test-data/unit/check-ignore.test
--- a/test-data/unit/check-ignore.test
+++ b/test-data/unit/check-ignore.test
@@ -246,6 +246,11 @@ IGNORE
 # type: ignore
 import MISSING
 
+[case testIgnoreWholeModulePy27]
+# flags: --python-version 2.7
+# type: ignore
+IGNORE
+
 [case testDontIgnoreWholeModule1]
 if True:
     # type: ignore
Repository: python/mypy

Issue:
Type inference regression involving empty collections
This fragment started generating an error due to #7810:
```py
from typing import Dict, Any
d: Dict[str, Any]
a = d["x"].get("y") or {}  # Need type annotation for 'a'
``` 

Since this seems like a pretty big regression, I'm going to revert the commit
that introduced it.

cc @msullivan 


Discussion:

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -3593,11 +3593,6 @@ def accept(self,
         """
         if node in self.type_overrides:
             return self.type_overrides[node]
-        # Don't use an Any type context, since that will cause more
-        # anys than we want for things that consume the inferred
-        # types.
-        if isinstance(get_proper_type(type_context), AnyType):
-            type_context = None
         self.type_context.append(type_context)
         try:
             if allow_none_return and isinstance(node, CallExpr):
diff --git a/mypy/suggestions.py b/mypy/suggestions.py
--- a/mypy/suggestions.py
+++ b/mypy/suggestions.py
@@ -30,9 +30,10 @@
 from mypy.state import strict_optional_set
 from mypy.types import (
     Type, AnyType, TypeOfAny, CallableType, UnionType, NoneType, Instance, TupleType,
-    TypeVarType, FunctionLike, TypedDictType, UninhabitedType,
+    TypeVarType, FunctionLike,
     TypeStrVisitor, TypeTranslator,
     is_optional, remove_optional, ProperType, get_proper_type,
+    TypedDictType
 )
 from mypy.build import State, Graph
 from mypy.nodes import (
@@ -721,9 +722,6 @@ def visit_tuple_type(self, t: TupleType) -> str:
         s = self.list_str(t.items)
         return 'Tuple[{}]'.format(s)
 
-    def visit_uninhabited_type(self, t: UninhabitedType) -> str:
-        return "Any"
-
     def visit_typeddict_type(self, t: TypedDictType) -> str:
         return t.fallback.accept(self)
 


Test patch:
diff --git a/test-data/unit/fine-grained-suggest.test b/test-data/unit/fine-grained-suggest.test
--- a/test-data/unit/fine-grained-suggest.test
+++ b/test-data/unit/fine-grained-suggest.test
@@ -803,23 +803,6 @@ z = foo(f(), g())
 (foo.List[Any], UNKNOWN) -> Tuple[foo.List[Any], Any]
 ==
 
-[case testSuggestDict]
-# suggest: foo.foo
-# suggest: foo.bar
-[file foo.py]
-def foo():
-    # return dict(x=5)
-    return {'x': 5}
-
-def bar():
-    return {}
-
-[builtins fixtures/dict.pyi]
-[out]
-() -> typing.Dict[str, int]
-() -> typing.Dict[Any, Any]
-==
-
 [case testSuggestWithErrors]
 # suggest: foo.foo
 [file foo.py]
diff --git a/test-data/unit/typexport-basic.test b/test-data/unit/typexport-basic.test
--- a/test-data/unit/typexport-basic.test
+++ b/test-data/unit/typexport-basic.test
@@ -672,7 +672,7 @@ from typing import Any
 a = [] # type: Any
 [builtins fixtures/list.pyi]
 [out]
-ListExpr(2) : builtins.list[<nothing>]
+ListExpr(2) : builtins.list[Any]
 
 [case testHigherOrderFunction]
 from typing import TypeVar, Callable, List
Repository: python/mypy

Issue:
Generate an error if trying to use @final with TypedDict
`@final` doesn't work as expected with TypedDicts, but mypy doesn't complain about it:

```py
from typing_extensions import final, TypedDict

@final  # No error here
class C(TypedDict):
    x: float
    y: int
    z: str

class D(C):  # No error here
    pass

```

This is confusing, since users would expect that `@final` prevents structural subtyping, but that's not the case. The simplest fix would be to generate an error if `@final` is used in a TypedDict definition.

Hints: This should be a pretty simple thing to fix.  Look at what happens in `analyze_class` in `mypy/semanal.py`.

Originally reported in #7845.


Discussion:

Resolving patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -1067,6 +1067,12 @@ def analyze_class(self, defn: ClassDef) -> None:
 
         is_typeddict, info = self.typed_dict_analyzer.analyze_typeddict_classdef(defn)
         if is_typeddict:
+            for decorator in defn.decorators:
+                decorator.accept(self)
+                if isinstance(decorator, RefExpr):
+                    if decorator.fullname in ('typing.final',
+                                              'typing_extensions.final'):
+                        self.fail("@final cannot be used with TypedDict", decorator)
             if info is None:
                 self.mark_incomplete(defn.name, defn)
             else:


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
@@ -1924,3 +1924,16 @@ v = {bad2: 2}  # E: Extra key 'bad' for TypedDict "Value"
 
 [builtins fixtures/dict.pyi]
 [typing fixtures/typing-full.pyi]
+
+[case testCannotUseFinalDecoratorWithTypedDict]
+from typing import TypedDict
+from typing_extensions import final
+
+@final  # E: @final cannot be used with TypedDict
+class DummyTypedDict(TypedDict):
+    int_val: int
+    float_val: float
+    str_val: str
+
+[builtins fixtures/dict.pyi]
+[typing fixtures/typing-full.pyi]
Repository: python/mypy

Issue:
Small typo in mypy/renaming.py
Should read usually rather than usuaully.


Discussion:

Resolving patch:
diff --git a/mypy/renaming.py b/mypy/renaming.py
--- a/mypy/renaming.py
+++ b/mypy/renaming.py
@@ -156,7 +156,7 @@ def visit_with_stmt(self, stmt: WithStmt) -> None:
         # convenience.  This is unsafe since with statements can affect control
         # flow by catching exceptions, but this is rare except for
         # assertRaises() and other similar functions, where the exception is
-        # raised by the last statement in the body, which usuaully isn't a
+        # raised by the last statement in the body, which usually isn't a
         # problem.
         stmt.body.accept(self)
 


Test patch:
Repository: python/mypy

Issue:
False positive on returning Any from function declared to return object
mypy with `--warn-return-any` gives a warning for this function:
```python
def f() -> object:
    x: Any = 1
    return x
```
This code is correct in fact: any type is `object`, so we can safely return `Any`.
It can be fixed like that:
```python
def f() -> object:
    x: Any = 1
    return cast(object, x)
```
but `cast(object, ...)` should be a no-op for mypy IMO.


Discussion:
I agree we can make an exception in this case. I think it is relatively low priority, but since the fix may be trivial I am setting priority to normal.

Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -3036,7 +3036,9 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
                         and not self.current_node_deferred
                         and not is_proper_subtype(AnyType(TypeOfAny.special_form), return_type)
                         and not (defn.name in BINARY_MAGIC_METHODS and
-                                 is_literal_not_implemented(s.expr))):
+                                 is_literal_not_implemented(s.expr))
+                        and not (isinstance(return_type, Instance) and
+                                 return_type.type.fullname == 'builtins.object')):
                         self.msg.incorrectly_returning_any(return_type, s)
                     return
 


Test patch:
diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test
--- a/test-data/unit/check-basic.test
+++ b/test-data/unit/check-basic.test
@@ -466,3 +466,11 @@ a = (a.A(), A())
 a.x  # E: "Tuple[a.A, b.A]" has no attribute "x"
 
 [builtins fixtures/dict.pyi]
+
+[case testReturnAnyFromFunctionDeclaredToReturnObject]
+# flags: --warn-return-any
+from typing import Any
+
+def f() -> object:
+    x: Any = 1
+    return x
Repository: python/mypy

Issue:
stubgen pyd file utf-8 error
Hello,

I have a file `_core.pyd` that comes from [QGIS](http://www.qgis.org) installation (file enclosed) that I can't use with stubgen:

```
Critical error during semantic analysis: mypy: can't decode file '_core.pyd': 'utf-8' codec can't decode byte 0x90 in position 2: invalid start byte
```
[_core.zip](https://github.com/python/mypy/files/3905454/_core.zip)




Discussion:
Stubgen doesn't support `.pyd` files. It would be nice if it would generate a better error message, however. Leaving this issue open because of the confusing error message.

Resolving patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -2004,9 +2004,11 @@ def parse_file(self) -> None:
                             self.path, os.strerror(ioerr.errno))],
                         module_with_blocker=self.id)
                 except (UnicodeDecodeError, DecodeError) as decodeerr:
-                    raise CompileError([
-                        "mypy: can't decode file '{}': {}".format(self.path, str(decodeerr))],
-                        module_with_blocker=self.id)
+                    if self.path.endswith('.pyd'):
+                        err = "mypy: stubgen does not support .pyd files: '{}'".format(self.path)
+                    else:
+                        err = "mypy: can't decode file '{}': {}".format(self.path, str(decodeerr))
+                    raise CompileError([err], module_with_blocker=self.id)
             else:
                 assert source is not None
                 self.source_hash = compute_hash(source)


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
@@ -1458,3 +1458,11 @@ test.py:4: error: Missing type parameters for generic type "_PathLike"
 test.py:4: note: Subscripting classes that are not generic at runtime may require escaping, see https://mypy.readthedocs.io/en/latest/common_issues.html#not-generic-runtime
 test.py:5: error: Missing type parameters for generic type "Queue"
 test.py:5: note: Subscripting classes that are not generic at runtime may require escaping, see https://mypy.readthedocs.io/en/latest/common_issues.html#not-generic-runtime
+
+[case testErrorMessageWhenOpenPydFile]
+# cmd: mypy a.pyd
+[file a.pyd]
+# coding: uft-8
+[out]
+mypy: stubgen does not support .pyd files: 'a.pyd'
+== Return code: 2
Repository: python/mypy

Issue:
stubgen: split @abstractproperty
From #7921: stubgen should split `@abstractproperty` into `@property @abstractmethod`, since the former has been deprecated since Python 3.3.


Discussion:

Resolving patch:
diff --git a/mypy/stubgen.py b/mypy/stubgen.py
--- a/mypy/stubgen.py
+++ b/mypy/stubgen.py
@@ -647,11 +647,14 @@ def process_name_expr_decorator(self, expr: NameExpr, context: Decorator) -> boo
                                                           'asyncio.coroutines',
                                                           'types'):
             self.add_coroutine_decorator(context.func, name, name)
-        elif any(self.refers_to_fullname(name, target)
-                 for target in ('abc.abstractmethod', 'abc.abstractproperty')):
+        elif self.refers_to_fullname(name, 'abc.abstractmethod'):
             self.add_decorator(name)
             self.import_tracker.require_name(name)
             is_abstract = True
+        elif self.refers_to_fullname(name, 'abc.abstractproperty'):
+            self.add_decorator('property')
+            self.add_decorator('abc.abstractmethod')
+            is_abstract = True
         return is_abstract
 
     def refers_to_fullname(self, name: str, fullname: str) -> bool:
@@ -674,8 +677,13 @@ def process_member_expr_decorator(self, expr: MemberExpr, context: Decorator) ->
               (expr.expr.name == 'abc' or
                self.import_tracker.reverse_alias.get('abc')) and
               expr.name in ('abstractmethod', 'abstractproperty')):
-            self.import_tracker.require_name(expr.expr.name)
-            self.add_decorator('%s.%s' % (expr.expr.name, expr.name))
+            if expr.name == 'abstractproperty':
+                self.import_tracker.require_name(expr.expr.name)
+                self.add_decorator('%s' % ('property'))
+                self.add_decorator('%s.%s' % (expr.expr.name, 'abstractmethod'))
+            else:
+                self.import_tracker.require_name(expr.expr.name)
+                self.add_decorator('%s.%s' % (expr.expr.name, expr.name))
             is_abstract = True
         elif expr.name == 'coroutine':
             if (isinstance(expr.expr, MemberExpr) and


Test patch:
diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test
--- a/test-data/unit/stubgen.test
+++ b/test-data/unit/stubgen.test
@@ -1625,7 +1625,8 @@ import abc
 from typing import Any
 
 class A(metaclass=abc.ABCMeta):
-    @abc.abstractproperty
+    @property
+    @abc.abstractmethod
     def x(self) -> Any: ...
 
 [case testAbstractProperty2_semanal]
@@ -1638,11 +1639,28 @@ class A:
 
 [out]
 import abc
-from abc import abstractproperty
 from typing import Any
 
 class A(metaclass=abc.ABCMeta):
-    @abstractproperty
+    @property
+    @abc.abstractmethod
+    def x(self) -> Any: ...
+
+[case testAbstractProperty3_semanal]
+import other
+from abc import abstractproperty as alias_name
+
+class A:
+    @alias_name
+    def x(self): pass
+
+[out]
+import abc
+from typing import Any
+
+class A(metaclass=abc.ABCMeta):
+    @property
+    @abc.abstractmethod
     def x(self) -> Any: ...
 
 [case testClassWithNameAnyOrOptional]
Repository: python/mypy

Issue:
stubgen generates __init__ with return type Any for partially annotated input
From this code, which typechecks
 
```python
class X:
  def __init__(self, x: str):
     pass
```
`stubgen` generates:

```python
from typing import Any

class X:
  def __init__(self, x: str) -> Any: ...
```
for which `mypy` reports:
```
out/repro.pyi:4: error: The return type of "__init__" must be None
Found 1 error in 1 file (checked 1 source file)
```

`stubgen` already has knowledge of `__init__` return value special case, when converting `__init__` without type annotations, the return value is annotated as `None`, but when `__init__` is partially annotated and lacks a type annotation for returned value, it is annotated as `Any`.

I don't know how much this practice of omitting type annotation for return value of `__init__` is common, but I noticed that there also are cases in some examples from pep048 (
https://www.python.org/dev/peps/pep-0484/#forward-references ).

If that makes sense, it seems easy to annotate `__init__`  with `None` also in the case where there are partial type annotations. It would also prevent the weird behaviour of generating stubs which do not typecheck from code which typechecks.

The selection of return type is implemented here https://github.com/python/mypy/blob/bbb192d9aab3803cb172ce4674c026e20606fcb0/mypy/stubgen.py#L604-L613

we could move the special casing of `__init__` around to also use `None` in that case. Please let me know if this looks correct and if you want me to submit a pull request for this.


Discussion:

Resolving patch:
diff --git a/mypy/stubgen.py b/mypy/stubgen.py
--- a/mypy/stubgen.py
+++ b/mypy/stubgen.py
@@ -602,14 +602,14 @@ def visit_func_def(self, o: FuncDef, is_abstract: bool = False) -> None:
                 arg = name + annotation
             args.append(arg)
         retname = None
-        if isinstance(o.unanalyzed_type, CallableType):
+        if o.name != '__init__' and isinstance(o.unanalyzed_type, CallableType):
             retname = self.print_annotation(o.unanalyzed_type.ret_type)
         elif isinstance(o, FuncDef) and (o.is_abstract or o.name in METHODS_WITH_RETURN_VALUE):
             # Always assume abstract methods return Any unless explicitly annotated. Also
             # some dunder methods should not have a None return type.
             retname = self.typing_name('Any')
             self.add_typing_import("Any")
-        elif o.name == '__init__' or not has_return_statement(o) and not is_abstract:
+        elif not has_return_statement(o) and not is_abstract:
             retname = 'None'
         retfield = ''
         if retname is not None:


Test patch:
diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test
--- a/test-data/unit/stubgen.test
+++ b/test-data/unit/stubgen.test
@@ -163,6 +163,14 @@ class C:
 class C:
     x: int = ...
 
+[case testInitTypeAnnotationPreserved]
+class C:
+    def __init__(self, x: str):
+        pass
+[out]
+class C:
+    def __init__(self, x: str) -> None: ...
+
 [case testSelfAssignment]
 class C:
     def __init__(self):
Repository: python/mypy

Issue:
mypy produces mangled output when run with a pty
version information:

```console
$ mypy --version
mypy 0.750
$ python3 --version --version
Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
[GCC 8.3.0]
```

test pty script:

```python
import errno
import os
import subprocess
import sys

x: int = 'nope'


class Pty(object):
    def __init__(self):
        self.r = self.w = None

    def __enter__(self):
        self.r, self.w = os.openpty()

        return self

    def close_w(self):
        if self.w is not None:
            os.close(self.w)
            self.w = None

    def close_r(self):
        assert self.r is not None
        os.close(self.r)
        self.r = None

    def __exit__(self, exc_type, exc_value, traceback):
        self.close_w()
        self.close_r()


def cmd_output_p(*cmd, **kwargs):
    with open(os.devnull) as devnull, Pty() as pty:
        kwargs = {'stdin': devnull, 'stdout': pty.w, 'stderr': pty.w}
        proc = subprocess.Popen(cmd, **kwargs)
        pty.close_w()

        buf = b''
        while True:
            try:
                bts = os.read(pty.r, 4096)
            except OSError as e:
                if e.errno == errno.EIO:
                    bts = b''
                else:
                    raise
            else:
                buf += bts
            if not bts:
                break

    return proc.wait(), buf, None


_, buf, _ = cmd_output_p(*sys.argv[1:])
sys.stdout.buffer.write(buf)
```

Output:

```console
$ python3 t.py mypy --pretty t.py
t.py:6: error:
Incompatible
types
in
assignment
(expression
has
type
"str",
variable
has
type
"int")
    x: int = 'nope'
             ^
Found 1 error in 1 file (checked 1 source file)
```

the output is colored (as expected) but has strange line breaks, almost like there's some wrapping logic that assumes the terminal is zero width or something (?)

![Screenshot from 2019-12-13 12-03-53](https://user-images.githubusercontent.com/1810591/70828653-a5b07800-1da0-11ea-9cc8-9ee562600ae3.png)

Interestingly, the output appears correct when not using `--pretty`:

```console
$ python3 t.py mypy t.py
t.py:6: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 1 error in 1 file (checked 1 source file)
```

going a bit further, here's some info that potentially confirms my suspicion:

```console
$ python3 t.py python3 -c 'import shutil; print(shutil.get_terminal_size())'
os.terminal_size(columns=0, lines=0)
```


Discussion:

Resolving patch:
diff --git a/mypy/util.py b/mypy/util.py
--- a/mypy/util.py
+++ b/mypy/util.py
@@ -424,9 +424,12 @@ def get_terminal_width() -> int:
     """Get current terminal width if possible, otherwise return the default one."""
     try:
         cols, _ = os.get_terminal_size()
-        return cols
     except OSError:
         return DEFAULT_COLUMNS
+    else:
+        if cols == 0:
+            return DEFAULT_COLUMNS
+        return cols
 
 
 def soft_wrap(msg: str, max_len: int, first_offset: int,


Test patch: