python / python/mypy

Cannot annotate child of a `Generic[AnyStr]` with an `Optional[AnyStr]` attribute

Open
#12,984 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Consider the following code:

from typing import AnyStr, Generic, Optional

class Base(Generic[AnyStr]):
    def __init__(self, data: AnyStr) -> None:
        self.data: AnyStr = data

class Child(Base):
    def __init__(self, data: AnyStr) -> None:
        super().__init__(data)
        self.buffer: Optional[AnyStr] = None

    def act(self, s: AnyStr) -> None:
        self.buffer = s

Running mypy on this code produces the following errors:

anystr01.py:13: error: Incompatible types in assignment (expression has type
"str", variable has type "Optional[AnyStr]")
            self.buffer = s
                          ^
anystr01.py:13: error: Incompatible types in assignment (expression has type
"bytes", variable has type "Optional[AnyStr]")
            self.buffer = s
                          ^
Found 2 errors in 1 file (checked 1 source file)

If I try to address the problem by changing the child class declaration to class Child(Base[AnyStr]):, mypy instead emits the following:

anystr01.py:9: error: Argument 1 to "__init__" of "Base" has incompatible type
"str"; expected "AnyStr"
            super().__init__(data)
                             ^
anystr01.py:9: error: Argument 1 to "__init__" of "Base" has incompatible type
"bytes"; expected "AnyStr"
            super().__init__(data)
                             ^
Found 2 errors in 1 file (checked 1 source file)

Is this a bug, or am I simply doing something wrong?

Your Environment

  • Mypy version used: both 0.961 and commit 132b8e527730b654a3f3d04684bebc9daff6cabd
  • Mypy command-line flags: --pretty
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.13
  • Operating system and version: macOS 11.6.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the supplied Python snippet as the reproduction case and run mypy with the reported flags. Trace generic inference and assignment checking for Child, Base[AnyStr], and Optional[AnyStr]; done means the behavior is corrected or clearly documented, with regression coverage for the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.