Skip to content

Incorrect / confusing invalid-method-override when overriding str.__iter__ #2612

@amartani

Description

@amartani

Summary

The following code results in an invalid-method-override error on the __iter__ method:

from __future__ import annotations

from collections.abc import Iterator

class MyStr(str):
    def __iter__(self) -> Iterator[str]:
        raise NotImplementedError

https://play.ty.dev/a0d43982-dd9a-4f49-bdbf-8d75d867a595

mypy / pyright do not report an error; pyrefly does report a similar error.

This seems to happen due to the fact that str.__iter__ is defined as an overload (with a LiteralString). Re-defining the overload clears the error:

from __future__ import annotations
from typing import LiteralString, overload

from collections.abc import Iterator

class MyStr(str):
    @overload
    def __iter__(self: LiteralString) -> Iterator[LiteralString]: ...
    @overload
    def __iter__(self) -> Iterator[str]: ...
    
    def __iter__(self) -> Iterator[str]:
        raise NotImplementedError

But this seems something that shouldn't be necessary.

Version

ty 0.0.13

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions