interperterbase: help type checkers do better type deduction

This assert causes several type checkers (both mypy and pyright) to
force `obj` to be a base `HoldableObject` instead of the specialized
object. Since the check itself may still be valuable as we don't have
fully type annotation coverage it's simply been removed when type
checking to aid in type specialization.
pull/9132/head
Dylan Baker 3 years ago
parent d8d09138c7
commit 580f316043
  1. 4
      mesonbuild/interpreterbase/baseobjects.py

@ -73,6 +73,10 @@ InterpreterObjectTypeVar = T.TypeVar('InterpreterObjectTypeVar', bound=HoldableO
class ObjectHolder(InterpreterObject, T.Generic[InterpreterObjectTypeVar]):
def __init__(self, obj: InterpreterObjectTypeVar, interpreter: 'Interpreter') -> None:
super().__init__(subproject=interpreter.subproject)
# This causes some type checkers to assume that obj is a base
# HoldableObject, not the specialized type, so only do this assert in
# non-type checking situations
if not T.TYPE_CHECKING:
assert isinstance(obj, HoldableObject), f'This is a bug: Trying to hold object of type `{type(obj).__name__}` that is not an `HoldableObject`'
self.held_object = obj
self.interpreter = interpreter

Loading…
Cancel
Save