Skip to content

python <3.8: math functions shouldn't accept any object with __index__ method #6213

Closed
@Akuli

Description

@Akuli

Runtime:

akuli@akuli-desktop:/tmp$ cat asd.py
class Foo:
    def __index__(self):
        return 1
akuli@akuli-desktop:/tmp$ python3.7 -c 'import math, asd; math.sin(asd.Foo())'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: must be real number, not Foo
akuli@akuli-desktop:/tmp$ python3.8 -c 'import math, asd; math.sin(asd.Foo())'
akuli@akuli-desktop:/tmp$ 

Stub:

_SupportsFloatOrIndex = Union[SupportsFloat, SupportsIndex]

def sin(__x: _SupportsFloatOrIndex) -> float: ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions