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

Issue:
error: Item "None" has no attribute "__bool__"
```python
thing = None
assert not thing.__bool__()
print(thing.__bool__())
```

As expected:
```
>python repro
False
```

Not expected:
```
>mypy repro
test:2: error: Item "None" has no attribute "__bool__"
test:3: error: Item "None" has no attribute "__bool__"
```

cf. python/typeshed#2432


Discussion:
This should be easy to fix by adding a special case for things that are defined on `None`, but not on `object`, which is apparently only `__bool__` on Python 3:
```
>>> set(dir(None)) - set(dir(object))
{'__bool__'}
```
Look at `checkmember.analyze_member_access` under this comment :-)
```
# The only attribute NoneType has are those it inherits from object
```

Resolving patch:
diff --git a/mypy/checkmember.py b/mypy/checkmember.py
--- a/mypy/checkmember.py
+++ b/mypy/checkmember.py
@@ -109,10 +109,19 @@ def analyze_member_access(name: str,
     elif isinstance(typ, NoneTyp):
         if chk.should_suppress_optional_error([typ]):
             return AnyType(TypeOfAny.from_error)
-        # The only attribute NoneType has are those it inherits from object
-        return analyze_member_access(name, builtin_type('builtins.object'), node, is_lvalue,
-                                     is_super, is_operator, builtin_type, not_ready_callback, msg,
-                                     original_type=original_type, chk=chk)
+        is_python_3 = chk.options.python_version[0] >= 3
+        # In Python 2 "None" has exactly the same attributes as "object". Python 3 adds a single
+        # extra attribute, "__bool__".
+        if is_python_3 and name == '__bool__':
+            return CallableType(arg_types=[],
+                                arg_kinds=[],
+                                arg_names=[],
+                                ret_type=builtin_type('builtins.bool'),
+                                fallback=builtin_type('builtins.function'))
+        else:
+            return analyze_member_access(name, builtin_type('builtins.object'), node, is_lvalue,
+                                         is_super, is_operator, builtin_type, not_ready_callback,
+                                         msg, original_type=original_type, chk=chk)
     elif isinstance(typ, UnionType):
         # The base object has dynamic type.
         msg.disable_type_names += 1


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
@@ -373,3 +373,9 @@ def foo(
 ):
     # type: (...) -> None
     pass
+
+[case testNoneHasBool]
+none = None
+b = none.__bool__()
+reveal_type(b)  # E: Revealed type is 'builtins.bool'
+[builtins fixtures/bool.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
@@ -333,3 +333,7 @@ def foo(
 ):
     # type: (...) -> None
     pass
+
+[case testNoneHasNoBoolInPython2]
+none = None
+b = none.__bool__() # E: "None" has no attribute "__bool__"
Repository: python/mypy

Issue:
Daemon crash on Windows (Python 3.6)
I get this crash when I try using dmypy on Python 3.6 (Windows 7):

```
C:\Users\jukka\python>python -m mypy.dmypy run -- --follow-imports=skip mypy\*.py
Traceback (most recent call last):
  File "C:\Users\jukka\python\venv\lib\site-packages\mypy-0.650+dev.0aa1d5cfda78863eec6b0f1f7b548842d85df1bd-py3.6.egg\mypy\dmypy.py", line 125, in main
    args.action(args)
  File "C:\Users\jukka\python\venv\lib\site-packages\mypy-0.650+dev.0aa1d5cfda78863eec6b0f1f7b548842d85df1bd-py3.6.egg\mypy\dmypy.py", line 241, in do_run
    start_server(args, allow_sources=True)
  File "C:\Users\jukka\python\venv\lib\site-packages\mypy-0.650+dev.0aa1d5cfda78863eec6b0f1f7b548842d85df1bd-py3.6.egg\mypy\dmypy.py", line 200, in start_server

    if daemonize(start_options, timeout=args.timeout, log_file=args.log_file):
  File "C:\Users\jukka\python\venv\lib\site-packages\mypy-0.650+dev.0aa1d5cfda78863eec6b0f1f7b548842d85df1bd-py3.6.egg\mypy\dmypy_server.py", line 64, in daemon
ize
    wShowWindow=0,  # SW_HIDE aka make the window invisible
TypeError: object() takes no parameters
```

Looks like `subprocess.STARTUPINFO` takes no arguments on Python 3.6 and earlier.


Discussion:
Oh, that is unfortunate. I suppose I will have to use ctypes instead of the wrapper.
Ah, this will be an easy fix, it looks like they just have a different interface. https://github.com/python/cpython/blob/3.4/Lib/subprocess.py#L398

Resolving patch:
diff --git a/mypy/dmypy_server.py b/mypy/dmypy_server.py
--- a/mypy/dmypy_server.py
+++ b/mypy/dmypy_server.py
@@ -58,9 +58,9 @@ def daemonize(options: Options,
         command = [sys.executable, '-m', 'mypy.dmypy', '--status-file', status_file, 'daemon']
         pickeled_options = pickle.dumps((options.snapshot(), timeout, log_file))
         command.append('--options-data="{}"'.format(base64.b64encode(pickeled_options).decode()))
-        info = STARTUPINFO(dwFlags=0x1,  # STARTF_USESHOWWINDOW aka use wShowWindow's value
-                           wShowWindow=0,  # SW_HIDE aka make the window invisible
-                           )
+        info = STARTUPINFO()
+        info.dwFlags = 0x1  # STARTF_USESHOWWINDOW aka use wShowWindow's value
+        info.wShowWindow = 0  # SW_HIDE aka make the window invisible
         try:
             subprocess.Popen(command,
                              creationflags=0x10,  # CREATE_NEW_CONSOLE


Test patch:
Repository: python/mypy

Issue:
New semantic analyzer: metaclasses



Discussion:
I think this also should be high priority because `ABCMeta` is used everywhere in typeshed and in fixtures, so it blocks progress in many tests.

Resolving patch:
diff --git a/mypy/newsemanal/semanal.py b/mypy/newsemanal/semanal.py
--- a/mypy/newsemanal/semanal.py
+++ b/mypy/newsemanal/semanal.py
@@ -842,8 +842,7 @@ def analyze_class(self, defn: ClassDef) -> None:
 
         bases = defn.base_type_exprs
 
-        # TODO: Support metaclasses
-        # self.update_metaclass(defn)
+        self.update_metaclass(defn)
         bases, tvar_defs, is_protocol = self.clean_up_bases_and_infer_type_variables(bases,
                                                                                      context=defn)
         # TODO: Support keyword arguments
@@ -1492,6 +1491,9 @@ def analyze_metaclass(self, defn: ClassDef) -> None:
                 #       and assume that the type object supports arbitrary
                 #       attributes, similar to an 'Any' base class.
                 return
+            if isinstance(sym.node, PlaceholderNode):
+                self.defer()
+                return
             if not isinstance(sym.node, TypeInfo) or sym.node.tuple_type is not None:
                 self.fail("Invalid metaclass '%s'" % metaclass_name, defn.metaclass)
                 return


Test patch:
diff --git a/test-data/unit/check-newsemanal.test b/test-data/unit/check-newsemanal.test
--- a/test-data/unit/check-newsemanal.test
+++ b/test-data/unit/check-newsemanal.test
@@ -725,3 +725,67 @@ class C: pass
 
 x: int
 reveal_type(x) # E: Revealed type is 'a.C'
+
+[case testNewAnalyzerMetaclass1]
+class A(metaclass=B):
+    pass
+
+class B(type):
+    def f(cls) -> int:
+        return 0
+
+reveal_type(A.f()) # E: Revealed type is 'builtins.int'
+
+[case testNewAnalyzerMetaclass2]
+reveal_type(A.f()) # E: Revealed type is 'builtins.int'
+
+class A(metaclass=B):
+    pass
+
+class AA(metaclass=C): # E: Metaclasses not inheriting from 'type' are not supported
+    pass
+
+class B(type):
+    def f(cls) -> int:
+        return 0
+
+class C: pass
+
+[case testNewAnalyzerMetaclassPlaceholder]
+class B(C): pass
+
+class A(metaclass=B):
+    pass
+
+class C(type):
+    def f(cls) -> int:
+        return 0
+
+reveal_type(A.f()) # E: Revealed type is 'builtins.int'
+
+[case testNewAnalyzerMetaclass1_python2]
+class A:
+    __metaclass__ = B
+
+reveal_type(A.f()) # E: Revealed type is 'builtins.int'
+
+class B(type):
+    def f(cls):
+        # type: () -> int
+        return 0
+
+[case testNewAnalyzerMetaclass2_python2]
+reveal_type(A.f()) # E: Revealed type is 'builtins.int'
+
+class A:
+    __metaclass__ = B
+
+class AA:
+    __metaclass__ = C  # E: Metaclasses not inheriting from 'type' are not supported
+
+class B(type):
+    def f(cls):
+        # type: () -> int
+        return 0
+
+class C: pass
Repository: python/mypy

Issue:
New semantic analyzer: properties



Discussion:

Resolving patch:
diff --git a/mypy/newsemanal/semanal.py b/mypy/newsemanal/semanal.py
--- a/mypy/newsemanal/semanal.py
+++ b/mypy/newsemanal/semanal.py
@@ -2858,11 +2858,9 @@ def visit_decorator(self, dec: Decorator) -> None:
                     self.fail("@final cannot be used with non-method functions", d)
         for i in reversed(removed):
             del dec.decorators[i]
-        if not dec.is_overload or dec.var.is_property:
-            if self.type:
-                dec.var.info = self.type
-                dec.var.is_initialized_in_class = True
-            self.add_symbol(dec.var.name(), dec, dec)
+        if (not dec.is_overload or dec.var.is_property) and self.type:
+            dec.var.info = self.type
+            dec.var.is_initialized_in_class = True
         if not no_type_check and self.recurse_into_functions:
             dec.func.accept(self)
         if dec.decorators and dec.var.is_property:


Test patch:
diff --git a/test-data/unit/check-newsemanal.test b/test-data/unit/check-newsemanal.test
--- a/test-data/unit/check-newsemanal.test
+++ b/test-data/unit/check-newsemanal.test
@@ -1054,3 +1054,24 @@ class Test:
     import a
     def __init__(self) -> None:
         some_module = self.a
+
+[case testNewAnalyzerProperty]
+class A:
+    @property
+    def x(self) -> B:
+        return 0  # E: Incompatible return value type (got "int", expected "B")
+
+    @property
+    def y(self) -> B:
+        pass
+
+    @y.setter
+    def y(self, x: B) -> None:
+        pass
+
+class B: pass
+
+a = A()
+reveal_type(a.x)  # E: Revealed type is '__main__.B'
+a.y = 1  # E: Incompatible types in assignment (expression has type "int", variable has type "B")
+[builtins fixtures/property.pyi]
diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test
--- a/test-data/unit/pythoneval.test
+++ b/test-data/unit/pythoneval.test
@@ -1368,5 +1368,4 @@ reveal_type(x['test'][0])
 typing.pyi:251: error: Class typing.Sequence has abstract attributes "__len__"
 typing.pyi:251: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
 ast.pyi:31: error: Name 'PyCF_ONLY_AST' already defined (possibly by an import)
-builtins.pyi:39: error: Name '__class__' already defined on line 39
 _testNewAnalyzerBasicTypeshed_newsemanal.py:4: error: Revealed type is 'builtins.int*'
Repository: python/mypy

Issue:
New semantic analyzer: overriding __init__ with Tuple base class
This generates a false positive:

```py
from typing import Tuple

c: C
class C(Tuple[int, str]):
    def __init__(self) -> None: pass  # Cannot overwrite NamedTuple attribute "__init__"
```

This error gets generated when processing typeshed builtins import cycle, so this is high priority.


Discussion:

Resolving patch:
diff --git a/mypy/newsemanal/semanal.py b/mypy/newsemanal/semanal.py
--- a/mypy/newsemanal/semanal.py
+++ b/mypy/newsemanal/semanal.py
@@ -947,7 +947,7 @@ def analyze_class_body_common(self, defn: ClassDef) -> None:
 
     def analyze_namedtuple_classdef(self, defn: ClassDef) -> bool:
         """Check if this class can define a named tuple."""
-        if defn.info and defn.info.tuple_type:
+        if defn.info and defn.info.is_named_tuple:
             # Don't reprocess everything. We just need to process methods defined
             # in the named tuple class body.
             is_named_tuple, info = True, defn.info  # type: bool, Optional[TypeInfo]
@@ -1277,11 +1277,10 @@ def configure_base_classes(self,
         base_types = []  # type: List[Instance]
         info = defn.info
 
+        info.tuple_type = None
         for base, base_expr in bases:
             if isinstance(base, TupleType):
-                # There may be an existing valid tuple type from previous semanal iterations.
-                # Use equality to check if it is the case.
-                if info.tuple_type and info.tuple_type != base:
+                if info.tuple_type:
                     self.fail("Class has two incompatible bases derived from tuple", defn)
                     defn.has_incompatible_baseclass = True
                 info.tuple_type = base


Test patch:
diff --git a/test-data/unit/check-newsemanal.test b/test-data/unit/check-newsemanal.test
--- a/test-data/unit/check-newsemanal.test
+++ b/test-data/unit/check-newsemanal.test
@@ -1636,3 +1636,10 @@ class Cls: ...  # E: Name 'Cls' already defined (possibly by an import)
 
 x: C
 class C: ...
+
+[case testNewAnalyzerTupleInit]
+from typing import Tuple
+
+c: C
+class C(Tuple[int, str]):
+    def __init__(self) -> None: pass
diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test
--- a/test-data/unit/pythoneval.test
+++ b/test-data/unit/pythoneval.test
@@ -1368,5 +1368,4 @@ reveal_type(x['test'][0])
 -- N.B: These errors are *wrong* and the new semantic analyzer can't yet
 -- fully analyze typeshed
 ast.pyi:31: error: Name 'PyCF_ONLY_AST' already defined (possibly by an import)
-codecs.pyi:49: error: Cannot overwrite NamedTuple attribute "__init__"
 _testNewAnalyzerBasicTypeshed_newsemanal.py:4: error: Revealed type is 'builtins.int*'
Repository: python/mypy

Issue:
New semantic analyzer: variable renaming
Support `--allow-redefinitions`. This should be simple -- we just need to run the renaming pass, probably in `mypy.build` right after parsing or after reachability analysis.


Discussion:

Resolving patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -59,6 +59,7 @@
 from mypy.fscache import FileSystemCache
 from mypy.metastore import MetadataStore, FilesystemMetadataStore, SqliteMetadataStore
 from mypy.typestate import TypeState, reset_global_state
+from mypy.renaming import VariableRenameVisitor
 
 from mypy.mypyc_hacks import BuildManagerBase
 
@@ -1916,6 +1917,9 @@ def semantic_analysis_pass1(self) -> None:
                 analyzer.visit_file(self.tree, self.xpath, self.id, options)
             # TODO: Do this while contructing the AST?
             self.tree.names = SymbolTable()
+            if options.allow_redefinition:
+                # Perform renaming across the AST to allow variable redefinitions
+                self.tree.accept(VariableRenameVisitor())
         else:
             # Do the first pass of semantic analysis: add top-level
             # definitions in the file to the symbol table.  We must do
diff --git a/mypy/newsemanal/semanal.py b/mypy/newsemanal/semanal.py
--- a/mypy/newsemanal/semanal.py
+++ b/mypy/newsemanal/semanal.py
@@ -4180,7 +4180,7 @@ def name_already_defined(self, name: str, ctx: Context,
             # Therefore its line number is always 1, which is not useful for this
             # error message.
             extra_msg = ' (by an import)'
-        elif node and node.line != -1 and self.cur_mod_id in split_module_names(node.fullname()):
+        elif node and node.line != -1 and self.is_local_name(node.fullname()):
             # TODO: Using previous symbol node may give wrong line. We should use
             #       the line number where the binding was established instead.
             extra_msg = ' on line {}'.format(node.line)
@@ -4188,6 +4188,10 @@ def name_already_defined(self, name: str, ctx: Context,
             extra_msg = ' (possibly by an import)'
         self.fail("Name '{}' already defined{}".format(unmangle(name), extra_msg), ctx)
 
+    def is_local_name(self, name: str) -> bool:
+        """Does name look like reference to a definition in the current module?"""
+        return self.cur_mod_id in split_module_names(name) or '.' not in name
+
     def fail(self, msg: str, ctx: Context, serious: bool = False, *,
              blocker: bool = False) -> None:
         if (not serious and


Test patch:
diff --git a/mypy/test/hacks.py b/mypy/test/hacks.py
--- a/mypy/test/hacks.py
+++ b/mypy/test/hacks.py
@@ -31,7 +31,6 @@
     'check-overloading.test',
     'check-protocols.test',
     'check-python2.test',
-    'check-redefine.test',
     'check-semanal-error.test',
     'check-serialize.test',
     'check-statements.test',
Repository: python/mypy

Issue:
~ is not working in mypy.ini/cache_dir
I'd like to create a unified mypy cache for all my projects and store it in my home directory.

Moreover, I'd like other developers, as well as CI scripts, to do the same - storing cache in THEIR home directories on their local machines.

For that, I specify in mypy.ini:

`[mypy]`
`cache_dir=~/.mypy_cache`

But that's not working as mypy creates directory `~` in the current directory instead of creating `.mypy_cache` in my home directory.

I'm using mypy 0.670 from pip on Ubuntu.


Discussion:
This seems a simple enough fix. Can you try your hand at sending us a PR?
Unfortunately, I couldn't find the clear place in the code to easily fix this.

Resolving patch:
diff --git a/mypy/main.py b/mypy/main.py
--- a/mypy/main.py
+++ b/mypy/main.py
@@ -1004,6 +1004,8 @@ def parse_section(prefix: str, template: Options,
         except ValueError as err:
             print("%s: %s: %s" % (prefix, key, err), file=sys.stderr)
             continue
+        if key == 'cache_dir':
+            v = os.path.expanduser(v)
         if key == 'silent_imports':
             print("%s: silent_imports has been replaced by "
                   "ignore_missing_imports=True; follow_imports=skip" % prefix, file=sys.stderr)


Test patch:
Repository: python/mypy

Issue:
When there is a duplicate module, show path of other module
It's easy to accidentally have two modules with the same name if a `__init__.py` file missing, for example. Mypy currently complains about one of the modules but it may not be obvious where the other conflicting module lives. It would be better to show the paths of both/all conflicting modules. Maybe also include a hint about possible missing `__init__.py` files.

Example:

```
foo.py
pkg/__init__.py
pkg/subpkg/foo.py
```

Note that `pkg/subpkg/__init__.py` is missing, which causes mypy to think that there are two instances of module `foo`.

Users get confused about this quite frequently, so we should perhaps prioritize this.


Discussion:
Looking at this issue


Resolving patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -2569,7 +2569,10 @@ def load_graph(sources: List[BuildSource], manager: BuildManager,
             continue
         if st.id in graph:
             manager.errors.set_file(st.xpath, st.id)
-            manager.errors.report(-1, -1, "Duplicate module named '%s'" % st.id)
+            manager.errors.report(
+                -1, -1,
+                "Duplicate module named '%s' (also at '%s')" % (st.id, graph[st.id].xpath)
+            )
             manager.errors.raise_error()
         graph[st.id] = st
         new.append(st)


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
@@ -106,7 +106,7 @@ mypy: can't decode file 'a.py': unknown encoding: uft-8
 [file two/mod/__init__.py]
 # type: ignore
 [out]
-two/mod/__init__.py: error: Duplicate module named 'mod'
+two/mod/__init__.py: error: Duplicate module named 'mod' (also at 'one/mod/__init__.py')
 == Return code: 2
 
 [case testFlagsFile]
Repository: python/mypy

Issue:
Show used config settings in verbose mode
Steps to reproduce:
1. Create `mypy.ini` with:
```
[mypy]
python_version = 2.7
```
2. Create a test file that uses Python 3 only type syntax, e.g.:
```
def f(x : int) -> int:
  return x
```
3. Run `python -mmypy test.py` where `python` in PATH is Python 3.

What I expected was for Python 3 to be used to typecheck the file. What actually happened was mypy somehow found a Python 2 executable on my system, and used that instead to typecheck the file. I thought this was unexpected, because I specifically ran mypy as `python -m mypy` so that it knew to use that python interpreter. Now, I know why this happened; because mypy is respecting the requested python version when it runs the typechecking. It would have been easier to debug if mypy by default emitted information about which Python it was using when it wasn't using the default.

Note: in the actual sequence of events, I didn't do (1), and didn't realize the file existed until I started digging.


Discussion:
Mypy never "selects" an interpreter to be interpreted (what would that even mean). Also the files are never executed by mypy (e.g. you don't need Python 2 interpreter to check Python 2 code).

However, mypy parses your code according to the syntax/grammar of Python version specified in the config file. And your example is not a valid Python 2.7.
Actually on the second thought, it seems to me we should show more info in verbose mode (currently it mostly useful for incremental mode).
I don't know too much about how mypy is implemented internally, so I was mostly guessing based on behavior. What I noticed is that if I run `python -mmypy test.py` it respects `python_version` in the config file, but when I run `python -mmypy --python-executable python3` it ignores `python_version` and parses the file as Python 3.
Yes, that's expected behavior; command-line flags override config file settings.
Sure. What's unexpected/confusing is the fact that they're different flags. `--python-executable` is documented to set `--python-version` if it is not already set, but a config file python-version doesn't count for the purposes of determining "if it's already set."
Is that still true in mypy 0.660? AFAIK we rationalized the behavior here. Perhaps the docs need tweaking?

Resolving patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -212,6 +212,7 @@ def _build(sources: List[BuildSource],
                            errors=errors,
                            flush_errors=flush_errors,
                            fscache=fscache)
+    manager.trace(repr(options))
 
     reset_global_state()
     try:


Test patch: