Skip to content

How do I type hint a static method as a class (static) attribute? #9317

Answered by erictraut
BaconPancakes asked this question in Q&A
Discussion options

You must be logged in to vote

why is a function definition not allowed to override an immutable Callable attribute?

As I mentioned above, overrides need to honor the LSP regardless of whether you access the attribute from the class object or from an instance of that class. This is required because Child is a subtype of Parent and type[Child] is a subtype of type[Parent].

from typing import Callable, reveal_type

class Parent:
    a: Callable[[int], int] = lambda x: x

    def __init__(self):
        self.a = lambda x: x

    @property
    def b(self) -> Callable[[int], int]:
        return lambda x: x

    def c(self, v: int, /) -> int:
        return v

class Child(Parent):
    @staticmethod
    def a(v: int, /) -> i…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@rayansostenes
Comment options

@BaconPancakes
Comment options

@BaconPancakes
Comment options

@erictraut
Comment options

Answer selected by BaconPancakes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants