79765302

Date: 2025-09-15 15:02:09
Score: 1.5
Natty:
Report link

You fail to consider subclasses:

(playground)

class B(A):
    def __bool__(self) -> bool:
        return False

reveal_type(foobar(B()))  # `int | None` at type checking time,
                          # but `B` at runtime.

To avoid this pitfall, mark A as @final:

(playground)

from typing import final

@final
class A: ...

As for a better way to write optional_obj and optional_obj.attribute, see How do I get Pylance to ignore the possibility of None?.

Reasons:
  • Blacklisted phrase (1): How do I
  • Probably link only (1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • High reputation (-2):
Posted by: InSync