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

Issue:
Deoptionalization not detected
Under `--strict-optional`, the following yields an error:

```
f=None  # type: Optional[Callable[..., None]]

# ...

if f is None:
    f = lambda *x: None
f()  # error: None not callable
```

Ideally, `f` should be understood not to be None at that point.


Discussion:
I have a similar problem:

    input = "10" # this can be either "" or a string containing any long value
    value = long(input,0) if input!="" else None
    if value==None or value>=0x1000000000000000:
        pass
    if value==None or long(value)>=0x1000000000000000:
        pass
    if value==None or (value!=None and value>=0x1000000000000000):
        pass

I get the following errors:

    mypytestcase.py:3: error: Unsupported operand types for <= ("int" and "Optional[int]")
    mypytestcase.py:5: error: Argument 1 to "int" has incompatible type "Optional[int]"; expected "SupportsInt"
    mypytestcase.py:7: error: Unsupported operand types for <= ("int" and "Optional[int]")

I my opinion, all of these options should work.
Probably, but have you tried replacing '== None' with 'is None' ?


Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -1772,6 +1772,12 @@ def infer_lambda_type_using_context(self, e: FuncExpr) -> CallableType:
         """
         # TODO also accept 'Any' context
         ctx = self.chk.type_context[-1]
+
+        if isinstance(ctx, UnionType):
+            callables = [t for t in ctx.items if isinstance(t, CallableType)]
+            if len(callables) == 1:
+                ctx = callables[0]
+
         if not ctx or not isinstance(ctx, CallableType):
             return None
 


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
@@ -518,3 +518,10 @@ if x is not None:
     x.x = 1 # OK here
 
 [builtins fixtures/ops.pyi]
+
+[case testOptionalLambdaInference]
+from typing import Optional, Callable
+f = None # type: Optional[Callable[[int], None]]
+f = lambda x: None
+f(0)
+[builtins fixtures/function.pyi]
Repository: python/mypy

Issue:
Unicode module docstring in Python 2
In Python 2, docstrings are str by default, but unicode docstrings generally work. However, mypy disallows them:

```
$ cat unicodedoc.py 
from __future__ import unicode_literals
__doc__ = "documentation"
$ python3 -m mypy --python-version 2.7 unicodedoc.py 
unicodedoc.py:2: error: Incompatible types in assignment (expression has type "unicode", variable has type "str")
```

This makes it harder to prepare a codebase for 2/3 compatibility with the future import for unicode_literals.

It looks like this behavior is hardcoded at https://github.com/python/mypy/blob/8c6ba06add03487f62ccc0a5bb188c43e6ca5a4f/mypy/nodes.py#L78. I can prepare a patch if people agree that a change is needed.


Discussion:
Yeah, this looks like a mypy bug. We'd be happy to receive a PR -- this should be pretty easy to fix.

Resolving patch:
diff --git a/mypy/nodes.py b/mypy/nodes.py
--- a/mypy/nodes.py
+++ b/mypy/nodes.py
@@ -75,7 +75,7 @@ def get_column(self) -> int: pass
 
 
 implicit_module_attrs = {'__name__': '__builtins__.str',
-                         '__doc__': '__builtins__.str',
+                         '__doc__': None,  # depends on Python version, see semanal.py
                          '__file__': '__builtins__.str',
                          '__package__': '__builtins__.str'}
 
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -2828,7 +2828,17 @@ def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -
 
         # Add implicit definitions of module '__name__' etc.
         for name, t in implicit_module_attrs.items():
-            v = Var(name, UnboundType(t))
+            # unicode docstrings should be accepted in Python 2
+            if name == '__doc__':
+                if self.pyversion >= (3, 0):
+                    typ = UnboundType('__builtins__.str')  # type: Type
+                else:
+                    typ = UnionType([UnboundType('__builtins__.str'),
+                                     UnboundType('__builtins__.unicode')])
+            else:
+                assert t is not None, 'type should be specified for {}'.format(name)
+                typ = UnboundType(t)
+            v = Var(name, typ)
             v._fullname = self.sem.qualified_name(name)
             self.sem.globals[name] = SymbolTableNode(GDEF, v, self.sem.cur_mod_id)
 


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
@@ -229,3 +229,14 @@ class Foo: pass
 
 [case testExec]
 exec('print 1 + 1')
+
+[case testUnicodeDocStrings]
+# flags: --python-version=2.7
+__doc__ = u"unicode"
+
+class A:
+    u"unicode"
+
+def f():
+    # type: () -> None
+    u"unicode"
Repository: python/mypy

Issue:
Can use deleted variable as a condition
Now we catch many cases where code tries to use a deleted variable, but this is still not caught:

```
x = 1
del x
if x: ...   # no error
```



Discussion:

Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -1763,6 +1763,10 @@ def visit_if_stmt(self, s: IfStmt) -> Type:
             for e, b in zip(s.expr, s.body):
                 t = self.accept(e)
                 self.check_usable_type(t, e)
+
+                if isinstance(t, DeletedType):
+                    self.msg.deleted_as_rvalue(t, s)
+
                 if self.options.strict_boolean:
                     is_bool = isinstance(t, Instance) and t.type.fullname() == 'builtins.bool'
                     if not (is_bool or isinstance(t, AnyType)):
@@ -1785,7 +1789,9 @@ def visit_if_stmt(self, s: IfStmt) -> Type:
 
     def visit_while_stmt(self, s: WhileStmt) -> Type:
         """Type check a while statement."""
-        self.accept_loop(IfStmt([s.expr], [s.body], None), s.else_body,
+        if_stmt = IfStmt([s.expr], [s.body], None)
+        if_stmt.set_line(s.get_line(), s.get_column())
+        self.accept_loop(if_stmt, s.else_body,
                          exit_condition=s.expr)
         return None
 


Test patch:
diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test
--- a/test-data/unit/check-statements.test
+++ b/test-data/unit/check-statements.test
@@ -964,6 +964,19 @@ del c.a
 c.a + 1
 [builtins fixtures/ops.pyi]
 
+[case testDelStatementWithConditions]
+x = 5
+del x
+if x: ...  # E: Trying to read deleted variable 'x'
+
+def f(x):
+    return x
+
+if 0: ...
+elif f(x): ...  # E: Trying to read deleted variable 'x'
+
+while x == 5: ...  # E: Trying to read deleted variable 'x'
+
 -- Yield statement
 -- ---------------
Repository: python/mypy

Issue:
Incompatible types in assignment with --strict-optional for a: Callable[..., None] = lambda *a, **k: None
This is a residue of https://github.com/python/mypy/pull/2634#issuecomment-271392350

This snippet causes an error when using `--strict-optional`:
```python
def f(a: Callable[..., None] = lambda *a, **k: None): pass
```
Error:
```
__tmp__.py:7: error: Incompatible types in assignment (expression has type Callable[[StarArg(Any), KwArg(Any)], None], variable has type Callable[..., None])
```
However this snippet is fine -- the only difference being that the return type is not `None`:
```python
def f(a: Callable[..., int] = lambda *a, **k: 0): pass
```
So @elazarg's claim "[it's not about the the parameters but about the return type](https://github.com/python/mypy/pull/2634#issuecomment-271436422)" is incorrect here.

And yes, this did break one place in our codebase. As a workaround I am putting `cast(Callable, ...)` around the `lambda` expression:
```python
def f(a: Callable[..., None] = cast(Callable, lambda *a, **k: None)): pass
```


Discussion:
 I am confused. This *is* about the return type, isn't it? 

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -1799,7 +1799,7 @@ def infer_lambda_type_using_context(self, e: FuncExpr) -> Optional[CallableType]
 
         if ARG_STAR in arg_kinds or ARG_STAR2 in arg_kinds:
             # TODO treat this case appropriately
-            return None
+            return callable_ctx
         if callable_ctx.arg_kinds != arg_kinds:
             # Incompatible context; cannot use it to infer types.
             self.chk.fail(messages.CANNOT_INFER_LAMBDA_TYPE, e)


Test patch:
diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test
--- a/test-data/unit/check-inference.test
+++ b/test-data/unit/check-inference.test
@@ -1033,7 +1033,18 @@ b = lambda: None  # type: Callable[[], None]
 f(b)
 g(b) # E: Argument 1 to "g" has incompatible type Callable[[], None]; expected Callable[[], int]
 
+[case testLambdaDefaultContext]
+# flags: --strict-optional
+from typing import Callable
+def f(a: Callable[..., None] = lambda *a, **k: None):
+    pass
+
+def g(a: Callable[..., None] = lambda *a, **k: 1):  # E: Incompatible return value type (got "int", expected None)
+    pass
+[out]
+main:6: error: Incompatible types in assignment (expression has type Callable[[StarArg(Any), KwArg(Any)], int], variable has type Callable[..., None])
 
+[builtins fixtures/dict.pyi]
 -- Boolean operators
 -- -----------------
Repository: python/mypy

Issue:
Signature of "__init_subclass__" incompatible with supertype "object"
Annotating additional arguments in the `__init_subclass__()`  causes the typechecking error:

`Signature of "__init_subclass__" incompatible with supertype "object"`.

Here is an example of annotated code derived from the [`__init_subclass__`](https://docs.python.org/3/reference/datamodel.html#object.__init_subclass__) example from the Python documentation:

```python
from typing import Dict, Any

class Extensible:
    def __init_subclass__(cls, name: str, **kwargs: Any) -> None:
        pass
```
Annotation `(cls, **kwargs: Dict[str,Any])` passes just fine.

Workaround to keep typechecker happy: don't use the explicit argument, just pull it from the `kwargs`.



Discussion:

Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -837,8 +837,9 @@ def check_method_or_accessor_override_for_base(self, defn: FuncBase,
         """Check if method definition is compatible with a base class."""
         if base:
             name = defn.name()
-            if name not in ('__init__', '__new__'):
-                # Check method override (__init__ and __new__ are special).
+            if name not in ('__init__', '__new__', '__init_subclass__'):
+                # Check method override
+                # (__init__, __new__, __init_subclass__ are special).
                 self.check_method_override_for_base_with_name(defn, name, base)
                 if name in nodes.inplace_operator_methods:
                     # Figure out the name of the corresponding operator method.


Test patch:
diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test
--- a/test-data/unit/check-classes.test
+++ b/test-data/unit/check-classes.test
@@ -303,6 +303,12 @@ class B(A):
     def g(self) -> None:
         def f(self) -> str: pass
 
+[case testOverride__init_subclass__WithDifferentSignature]
+class A:
+    def __init_subclass__(cls, x: int) -> None: pass
+class B(A):
+    def __init_subclass__(cls) -> None: pass
+
 
 -- Constructors
 -- ------------
Repository: python/mypy

Issue:
Covariant type variables should be allowed in __init__
There are several issues about covariance, but I didn't find any discussion about ``__init__``. Currently, mypy complains about this code:
```python
from typing import Generic, TypeVar

T_co = TypeVar('T_co', covariant=True)

class C(Generic[T_co]):
    def __init__(self, x: T_co) -> None:
       # Error: Cannot use a covariant type variable as a parameter
       ...
```
Although this is normal in general, I believe this should be allowed for ``__init__``. Otherwise it would be difficult (if possible at all) to put something into a covariant (immutable) container on instantiation.


Discussion:
Yeah, this should be allowed. Not sure if there are some additional edge cases related to `__init__` we should handle differently once we allow this -- `x.__init__(...)` is already rejected, which is good.

Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -582,7 +582,10 @@ def is_implicit_any(t: Type) -> bool:
                     elif isinstance(arg_type, TypeVarType):
                         # Refuse covariant parameter type variables
                         # TODO: check recursively for inner type variables
-                        if arg_type.variance == COVARIANT:
+                        if (
+                            arg_type.variance == COVARIANT and
+                            defn.name() not in ('__init__', '__new__')
+                        ):
                             self.fail(messages.FUNCTION_PARAMETER_CANNOT_BE_COVARIANT, arg_type)
                     if typ.arg_kinds[i] == nodes.ARG_STAR:
                         # builtins.tuple[T] is typing.Tuple[T, ...]


Test patch:
diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test
--- a/test-data/unit/check-classes.test
+++ b/test-data/unit/check-classes.test
@@ -1942,6 +1942,21 @@ def new_pro_user(user_class: Type[ProUser]):
     new_user(user_class)
 [out]
 
+[case testAllowCovariantArgsInConstructor]
+from typing import Generic, TypeVar
+
+T_co = TypeVar('T_co', covariant=True)
+
+class C(Generic[T_co]):
+    def __init__(self, x: T_co) -> None: # This should be allowed
+        self.x = x
+    def meth(self) -> None:
+        reveal_type(self.x) # E: Revealed type is 'T_co`1'
+
+reveal_type(C(1).x) # E: Revealed type is 'builtins.int*'
+[builtins fixtures/property.pyi]
+[out]
+
 [case testTypeUsingTypeCErrorCovariance]
 from typing import Type, TypeVar
 class User: pass
Repository: python/mypy

Issue:
Accept class with Any base as a metaclass
This is rejected, though it should be okay:

```
from typing import Any
metabase = None  # type: Any
class M(metabase): pass
class C(object):
  __metaclass__ = M
```

(Reported by @gvanrossum in https://github.com/python/mypy/pull/2830#issuecomment-282442517.)


Discussion:

Resolving patch:
diff --git a/mypy/nodes.py b/mypy/nodes.py
--- a/mypy/nodes.py
+++ b/mypy/nodes.py
@@ -2026,7 +2026,8 @@ def calculate_metaclass_type(self) -> 'Optional[mypy.types.Instance]':
         return None
 
     def is_metaclass(self) -> bool:
-        return self.has_base('builtins.type') or self.fullname() == 'abc.ABCMeta'
+        return (self.has_base('builtins.type') or self.fullname() == 'abc.ABCMeta' or
+                self.fallback_to_any)
 
     def _calculate_is_enum(self) -> bool:
         """


Test patch:
diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test
--- a/test-data/unit/check-classes.test
+++ b/test-data/unit/check-classes.test
@@ -2950,3 +2950,27 @@ class B(metaclass=MM): # E: Invalid metaclass 'MM'
     y = 0
 reveal_type(A.y) # E: Revealed type is 'builtins.int'
 A.x # E: "A" has no attribute "x"
+
+[case testAnyAsBaseOfMetaclass]
+from typing import Any, Type
+M = None  # type: Any
+class MM(M): pass
+
+class A(metaclass=MM):
+    y = 0
+    @classmethod
+    def f(cls) -> None: pass
+    def g(self) -> None: pass
+
+def h(a: Type[A], b: Type[object]) -> None:
+    h(a, a)
+    h(b, a) # E: Argument 1 to "h" has incompatible type Type[object]; expected Type[A]
+    a.f(1) # E: Too many arguments for "f" of "A"
+    reveal_type(a.y) # E: Revealed type is 'builtins.int'
+
+x = A # type: MM
+reveal_type(A.y) # E: Revealed type is 'builtins.int'
+reveal_type(A.x) # E: Revealed type is 'Any'
+A.f(1) # E: Too many arguments for "f" of "A"
+A().g(1) # E: Too many arguments for "g" of "A"
+[builtins fixtures/classmethod.pyi]
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
@@ -292,3 +292,10 @@ class A(object):
     y = 0
 reveal_type(A.y) # E: Revealed type is 'builtins.int'
 A.x # E: "A" has no attribute "x"
+
+[case testAnyAsBaseOfMetaclass]
+from typing import Any, Type
+M = None  # type: Any
+class MM(M): pass
+class A(object):
+    __metaclass__ = MM
Repository: python/mypy

Issue:
sys.version_info is assumed to be (3, 5), even on Python 3.6
Running Python 3.6, I noticed that any type hints that I add to the stub files with a `if sys.version_info >= (3, 6):` condition, don't have any effect.

After investigating this a little, it seems that [semanal.py](https://github.com/python/mypy/blob/ae47a3edc5ff69845398cbefa7652b51362f5c1c/mypy/semanal.py#L3030) uses the `PYTHON3_VERSION` value inside [default.py](https://github.com/python/mypy/blob/452a64fd1a361204a3123abca57a312e15baa9a3/mypy/defaults.py#L2) module as sys.version_info and it is currently hardcoded as `3.5`.

Using `mypy.ini` config file with the following content fixes the issue:

```
[mypy]
    python_version = 3.6
```

But still the default behaviour is confusing and probably should be changed.




Discussion:

Resolving patch:
diff --git a/mypy/defaults.py b/mypy/defaults.py
--- a/mypy/defaults.py
+++ b/mypy/defaults.py
@@ -1,4 +1,4 @@
 PYTHON2_VERSION = (2, 7)
-PYTHON3_VERSION = (3, 5)
+PYTHON3_VERSION = (3, 6)
 CACHE_DIR = '.mypy_cache'
 CONFIG_FILE = 'mypy.ini'


Test patch:
Repository: python/mypy

Issue:
When `a` has type `Any`, after `assert a is None`, the rest of the block is considered dead code
Steps to reproduce:

1. Check out [this repo](https://github.com/benkuhn/mypy-repro)
2. Install the packages from `requirements.txt`
3. Run `mypy repro.py` from the repo root.

Expected output:
```
repro.py:4: error: Revealed type is 'Any'
repro.py:6: error: Revealed type is 'builtins.None'
```

Actual output:
```
repro.py:4: error: Revealed type is 'Any'
```

The issue goes away if you disable strict Optional checking.

This is preventing a significant amount of our codebase from being checked right now, so I would appreciate any suggestions for a workaround!


Discussion:

Resolving patch:
diff --git a/mypy/meet.py b/mypy/meet.py
--- a/mypy/meet.py
+++ b/mypy/meet.py
@@ -69,6 +69,10 @@ class C(A, B): ...
     TODO: Don't consider callables always overlapping.
     TODO: Don't consider type variables with values always overlapping.
     """
+    # Any overlaps with everything
+    if isinstance(t, AnyType) or isinstance(s, AnyType):
+        return True
+
     # Since we are effectively working with the erased types, we only
     # need to handle occurrences of TypeVarType at the top level.
     if isinstance(t, TypeVarType):


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
@@ -77,6 +77,15 @@ else:
   reveal_type(x)  # E: Revealed type is 'builtins.int'
 [builtins fixtures/bool.pyi]
 
+[case testAnyCanBeNone]
+from typing import Optional, Any
+x = None  # type:  Any
+if x is None:
+  reveal_type(x)  # E: Revealed type is 'builtins.None'
+else:
+  reveal_type(x)  # E: Revealed type is 'Any'
+[builtins fixtures/bool.pyi]
+
 [case testOrCases]
 from typing import Optional
 x = None  # type: Optional[str]
Repository: python/mypy

Issue:
Version doesn't include git commit hash when installed via "pip3 install ."
When running mypy directly out of the git repo, the version (reported by `-V` or `--version`) includes the git commit hash. It would be nice if it was also included when mypy was installed directly from the repo, e.g. using `pip3 install .` or perhaps via `pip3 install git+git://github.com/python/mypy.git`. However we should make sure that when we actually build a distribution file, that file only includes the base version. (Or perhaps only/exactly when the HEAD revision matches a tag whose name matches the base version?)

(See also PR #2452.)


Discussion:
Note that the version is especially important for `-i` mode, since the cache cannot be trusted when a different version is installed.

Also note that installing from a git-labeled version should not show the commit hash but installing from something intermediate should always show the commit hash. Or something like that. The idea is that when we build a distribution and push it to PyPI it should not have the commit hash in the version but when we install locally (e.g. using `pip3 install -U .`) the commit hash should be included.
To clarify, as of right now:

- `pip install -e .` (or just setting PYTHONPATH to point at mypy)  includes commit hash
- `python setup.py bdist_wheel` does not include commit hash

We want to keep the above unchanged.

We want this to happen:
- `pip install /local/repo`, and `pip install git+git://github.com/python/mypy.git` should also include commit hash
- building the wheel should complain if version hard coded in `version.py` doesn't match the HEAD commit tag

Correct?

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -14,12 +14,15 @@
 # alternative forms of installing, as suggested by README.md).
 from setuptools import setup
 from setuptools.command.build_py import build_py
-from mypy.version import base_version
+from mypy.version import base_version, __version__
 from mypy import git
 
 git.verify_git_integrity_or_abort(".")
 
-version = base_version
+if any(dist_arg in sys.argv[1:] for dist_arg in ('bdist_wheel', 'sdist')):
+    version = base_version
+else:
+    version = __version__
 description = 'Optional static typing for Python'
 long_description = '''
 Mypy -- Optional Static Typing for Python


Test patch: