Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3.11 StrEnum error when member is named "title" #14494

Closed
samypr100 opened this issue Jan 21, 2023 · 4 comments
Closed

Python 3.11 StrEnum error when member is named "title" #14494

samypr100 opened this issue Jan 21, 2023 · 4 comments
Labels
bug mypy got something wrong topic-enum

Comments

@samypr100
Copy link

samypr100 commented Jan 21, 2023

Bug Report

Currently when running mypy==0.991 on a Python 3.11 file that's using StrEnum, it errors unexpectedly on an specific named member on my code. In this case, the member only needed to be named title to get an Incompatible types in assignment error.

I realize python 3.11 support gh-12840 is still ongoing, but I thought I should report it to help others who might encounter this.

It might also be worth noting that it happens in older Python versions (tested on 3.10) when using strenum backport.

To Reproduce

from enum import StrEnum


class TheBug0(StrEnum):
    lorem = "foo"
    ipsum = "ipsum"
    title = "bug0"  # Bug because left side is named "title"?


class TheBug1(StrEnum):
    lorem = "foo"
    title = "bug1"  # Bug because left side is named "title"?
    ipsum = "bar"


class TheBug2(StrEnum):
    title = "bug2"  # Bug because left side is named "title"?
    lorem = "foo"
    ipsum = "bar"


class NoBug(StrEnum):
    lorem = "foo"
    ipsum = "bar"

Playground Gist URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=910f75dbbd0c2ca484dc018a1983b654

Expected Behavior

No Errors

Actual Behavior

main.py:7: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], str]")  [assignment]
main.py:12: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], str]")  [assignment]
main.py:17: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], str]")  [assignment]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: mypy main.py
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.1
@samypr100 samypr100 added the bug mypy got something wrong label Jan 21, 2023
@ngnpope
Copy link

ngnpope commented Jan 23, 2023

Interesting... This is because enum.StrEnum is a subclass of str, so the title member ends up shadowing str.title().

In fact, the same can be observed when trying to use member names that shadow other methods of str:

from enum import StrEnum


class BuggyEnum(StrEnum):
    capitalize = "capitalize"
    casefold = "casefold"
    center = "center"
    count = "count"
    encode = "encode"
    endswith = "endswith"
    expandtabs = "expandtabs"
    find = "find"
    format = "format"
    format_map = "format_map"
    index = "index"
    isalnum = "isalnum"
    isalpha = "isalpha"
    isascii = "isascii"
    isdecimal = "isdecimal"
    isdigit = "isdigit"
    isidentifier = "isidentifier"
    islower = "islower"
    isnumeric = "isnumeric"
    isprintable = "isprintable"
    isspace = "isspace"
    istitle = "istitle"
    isupper = "isupper"
    join = "join"
    ljust = "ljust"
    lower = "lower"
    lstrip = "lstrip"
    maketrans = "maketrans"
    partition = "partition"
    removeprefix = "removeprefix"
    removesuffix = "removesuffix"
    replace = "replace"
    rfind = "rfind"
    rindex = "rindex"
    rjust = "rjust"
    rpartition = "rpartition"
    rsplit = "rsplit"
    rstrip = "rstrip"
    split = "split"
    splitlines = "splitlines"
    startswith = "startswith"
    strip = "strip"
    swapcase = "swapcase"
    title = "title"
    translate = "translate"
    upper = "upper"
    zfill = "zfill"

This chucks out the following:

main.py:5: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], str]")  [assignment]
main.py:6: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], str]")  [assignment]
main.py:7: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, SupportsIndex, str], str]")  [assignment]
main.py:8: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str, Optional[SupportsIndex], Optional[SupportsIndex]], int]")  [assignment]
main.py:9: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str, str], bytes]")  [assignment]
main.py:10: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, Union[str, Tuple[str, ...]], Optional[SupportsIndex], Optional[SupportsIndex]], bool]")  [assignment]
main.py:11: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, SupportsIndex], str]")  [assignment]
main.py:12: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str, Optional[SupportsIndex], Optional[SupportsIndex]], int]")  [assignment]
main.py:13: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, VarArg(object), KwArg(object)], str]")  [assignment]
main.py:14: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, _FormatMapMapping], str]")  [assignment]
main.py:15: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str, Optional[SupportsIndex], Optional[SupportsIndex]], int]")  [assignment]
main.py:16: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:17: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:18: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:19: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:20: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:21: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:22: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:23: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:24: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:25: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:26: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:27: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], bool]")  [assignment]
main.py:28: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, Iterable[str]], str]")  [assignment]
main.py:29: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, SupportsIndex, str], str]")  [assignment]
main.py:30: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], str]")  [assignment]
main.py:31: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, Optional[str]], str]")  [assignment]
main.py:32: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as overloaded function)  [assignment]
main.py:33: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str], Tuple[str, str, str]]")  [assignment]
main.py:34: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str], str]")  [assignment]
main.py:35: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str], str]")  [assignment]
main.py:36: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str, str, SupportsIndex], str]")  [assignment]
main.py:37: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str, Optional[SupportsIndex], Optional[SupportsIndex]], int]")  [assignment]
main.py:38: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str, Optional[SupportsIndex], Optional[SupportsIndex]], int]")  [assignment]
main.py:39: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, SupportsIndex, str], str]")  [assignment]
main.py:40: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, str], Tuple[str, str, str]]")  [assignment]
main.py:41: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, Optional[str], SupportsIndex], List[str]]")  [assignment]
main.py:42: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, Optional[str]], str]")  [assignment]
main.py:43: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, Optional[str], SupportsIndex], List[str]]")  [assignment]
main.py:44: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, bool], List[str]]")  [assignment]
main.py:45: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, Union[str, Tuple[str, ...]], Optional[SupportsIndex], Optional[SupportsIndex]], bool]")  [assignment]
main.py:46: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, Optional[str]], str]")  [assignment]
main.py:47: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], str]")  [assignment]
main.py:48: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], str]")  [assignment]
main.py:49: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, _TranslateTable], str]")  [assignment]
main.py:50: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str], str]")  [assignment]
main.py:51: error: Incompatible types in assignment (expression has type "str", base class "str" defined the type as "Callable[[str, SupportsIndex], str]")  [assignment]
Found 47 errors in 1 file (checked 1 source file)

https://mypy-play.net/?mypy=latest&python=3.11&gist=d21b0840b2caefcb83142ca1f0d0b7ca

@JelleZijlstra
Copy link
Member

This error is correct. You have an incompatible override, which creates unsound code.

Consider:

def title_string(s: str) -> str:
    return s.title()

s(TheBug0.title)  # runtime error

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Jan 23, 2023
@ngnpope
Copy link

ngnpope commented Jan 23, 2023

Probably something that should be mentioned under the limitations in the documentation, if anyone cares enough to write a patch... 🤷🏻

@samypr100
Copy link
Author

samypr100 commented Apr 19, 2023

Note, this behavior should now fixed upstream due to python/cpython#103600

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-enum
Projects
None yet
Development

No branches or pull requests

4 participants