Skip to content

Prevent unbound trace due to type hints #6809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pymc/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from pytensor.tensor.random.type import RandomType
from pytensor.tensor.sharedvar import ScalarSharedVariable
from pytensor.tensor.var import TensorConstant, TensorVariable
from typing_extensions import Self

from pymc.blocking import DictToArrayBijection, RaveledVars
from pymc.data import GenTensorVariable, is_minibatch
Expand Down Expand Up @@ -213,7 +214,9 @@ def __init_subclass__(cls, **kwargs):
# Initialize object in its own context...
# Merged from InitContextMeta in the original.
def __call__(cls, *args, **kwargs):
instance = cls.__new__(cls, *args, **kwargs)
# We type hint Model here so type checkers understand that Model is a context manager.
# This metaclass is only used for Model, so this is safe to do. See #6809 for more info.
instance: "Model" = cls.__new__(cls, *args, **kwargs)
with instance: # appends context
instance.__init__(*args, **kwargs)
return instance
Expand Down Expand Up @@ -478,10 +481,10 @@ def __init__(self, mean=0, sigma=1, name=''):

if TYPE_CHECKING:

def __enter__(self: "Model") -> "Model":
def __enter__(self: Self) -> Self:
...

def __exit__(self: "Model", *exc: Any) -> bool:
def __exit__(self, exc_type: None, exc_val: None, exc_tb: None) -> None:
...

def __new__(cls, *args, **kwargs):
Expand Down