Skip to content

imprecise types on urllib3.Retry.new / urllib3.Retry.increment #3363

Closed
@asottile-sentry

Description

Subject

when attempting to subclass urllib3.Retry -- one would want to implement def increment(...) -> Self: as both increment and new preserve the type but label the return value as Retry (essentially breaking subclassing types):

def new(self, **kw: typing.Any) -> Retry:

if the codebase were python 3.11+ I would use from typing import Self -- it appears elsewhere in the codebase one-off SelfT TypeVars are created -- that pattern could be followed here

alternatively the TYPE_CHECKING trick could be used and is understood by the current major type checkers:

if TYPE_CHECKING:
    from typing_extensions import Self

Environment

N/A -- latest primary branch source, mypy 1.9

Steps to Reproduce

from typing import Any, Self
import urllib3
class MyRetry(urllib3.Retry):
    def increment(self, *a: Any, **k: Any) -> Self:  # type error on this line!
        return super().increment(*a, **k)
$ mypy t.py
t.py:5: error: Incompatible return value type (got "Retry", expected "Self")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

no errors

Actual Behavior

shown above

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions