Python 3.11 StrEnum error when member is named "title" #14494
Copy link
Copy link
Closed as not planned
Labels
Description
Activity
Interesting... This is because
enum.StrEnumis a subclass ofstr, so thetitlemember ends up shadowingstr.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
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 errorReacted by Alex Waygood, samypr100, Nick Pope, Neil Girdhar and Ethan FurmanProbably something that should be mentioned under the limitations in the documentation, if anyone cares enough to write a patch... 🤷🏻
Reacted by samypr100Note, this behavior should now fixed upstream due to python/cpython#103600
- added a commit that references this issue
on Feb 24, 2025
Bug Report
Currently when running
mypy==0.991on a Python 3.11 file that's usingStrEnum, it errors unexpectedly on an specific named member on my code. In this case, the member only needed to be namedtitleto get anIncompatible types in assignmenterror.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
Playground Gist URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=910f75dbbd0c2ca484dc018a1983b654
Expected Behavior
No Errors
Actual Behavior
Your Environment
0.991mypy main.pymypy.ini(and other config files):None3.11.1