Skip to content

TypeVar does not support unary OP despite being upper bounded by type supporting unary OP #1972

@randolf-scholz

Description

@randolf-scholz

Describe the Bug

This is basically the same error as #1720, but for unary operators:

inversion sandbox

from typing import Self

class Foo:
    def __invert__(self) -> Self:
        return self

def test[F: Foo](foo: F) -> F:
    return ~foo  # ❌️ Unary `~` is not supported on `F`

unary minus sandbox

from typing import Self

class Foo:
    def __neg__(self) -> Self:
        return self

def test[F: Foo](foo: F) -> F:
    return -foo  # ❌️ Unary `-` is not supported on `F`

unary plus sandbox

from typing import Self

class Foo:
    def __pos__(self) -> Self:
        return self

def test[F: Foo](foo: F) -> F:
    return +foo  # ❌️ Unary `+` is not supported on `F`

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0BlGFDAAddOIDGUVHDh0AYrlyJxddXUwwwdAPq6eAd30AKOMLAAaOkZiVEndAwCUdALQA%2BQRdXoN-ukoYBgBXSj9zEXFxLR0GeAYAbQUHJVwAXRMwZVTXT0VffyDQ8Lps3DoAKkq6ACYQSxAQhmg4EnJEEABiOgBVFqgIJjKQ9EkW3HQ4aKxtMt4aVAZddBCabDsTfAdWF3cvOAZ7NQ1isL8xEAA5NY37OmB8AF9REHEGkDIgsChSQgZaFAKD0AAqkb6-OhoLB4fB0SSTSBsMJLCCTQjiHpCGB0AAWDAYxDgiAA9CSvtpfoReGwSTB0CTMLhJHASQj0EiURMGfNKHRUAA3VDQVDYWDwxEQZGUVGTOi4YjctriMgMXGTNwCuxwNF%2BAC8dFeAGZCABGWqvd5PRqocYQLUKaAwCjQnAEdogJ5AA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions