Skip to content

Weird behavior with Optional[List[<smth>]] as classmethod return type and value produced from Any as part of that list #7612

Closed
@actionless

Description

@actionless
  • Are you reporting a bug, or opening a feature request?
    Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

from typing import List, Optional, Any


class MyClass1:

    _result: Optional[List[str]] = None
    _foo: Any = None

    @classmethod
    def get(cls, some_arg: Optional[str]) -> List[str]:
        if not cls._result:
            splitted_foo: List[str] = (
                cls._foo.split(',') if cls._foo else []
            )
            cls._result = [some_arg or 'a'] + splitted_foo
        return cls._result


class MyClass2:

    _result: Optional[List[str]] = None
    _foo: Any = None

    @classmethod
    def get(cls) -> List[str]:
        if not cls._result:
            cls._result = (
                cls._foo.split(',') if cls._foo else []
            )
        return cls._result


class MyClass3:

    _result: Optional[List[str]] = None
    _foo: Any = None

    @classmethod
    def get(cls, some_arg: Optional[str]) -> List[str]:
        if not cls._result:
            cls._result = [some_arg or 'a'] + (
                cls._foo.split(',') if cls._foo else []
            )
        return cls._result
#       ^ [mypy] : Incompatible return value type (got "Optional[List[str]]", expected "List[str]")


class MyClass4:

    _result: Optional[List[str]] = None
    _foo: Optional[str] = None

    @classmethod
    def get(cls, some_arg: Optional[str]) -> List[str]:
        if not cls._result:
            cls._result = [some_arg or 'a'] + (
                cls._foo.split(',') if cls._foo else []
            )
        return cls._result
  • What is the actual behavior/output?
(env) $ mypy --version
mypy 0.720
(env) $ mypy mypy_func.py
mypy_func.py:44: error: Incompatible return value type (got "Optional[List[str]]", expected "List[str]")

(env) $ pip install git+https://github.com/python/mypy/
.....
      Successfully uninstalled mypy-0.720
Successfully installed mypy-0.740+dev.6c4599dbdfe3bcc721dd480bc82e055e37102184
(env) $ mypy mypy_func.py
mypy_func.py:44: error: Incompatible return value type (got "Optional[List[str]]", expected "List[str]")
Found 1 error in 1 file (checked 1 source file)

(env) $ deactivate
$ mypy --version
mypy 0.730
$ mypy mypy_func.py
mypy_func.py:44: error: Incompatible return value type (got "Optional[List[str]]", expected "List[str]")
Found 1 error in 1 file (checked 1 source file)
  • What is the behavior/output you expect?
    without that error, i think the right error should be what 'Any' type doesn't have '.split()' method

But important to note what Class1 and Class2 not causing any mypy warnings, while Class3 does: it seems to me what all 3 of them have the same problem so should be handled with the same result.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?
$ python --version
Python 3.7.4
  • What are the mypy flags you are using? (For example --strict-optional)
    none of them

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions