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

Conversation

thomasaarholt
Copy link
Contributor

@thomasaarholt thomasaarholt commented Jul 2, 2023

There are currently two type errors in the Model class, preventing smooth usage with type checkers (like in VSCode):
This one became present in recent versions of pyright:

# script.py
import pymc as pm
import arviz as az

with pm.Model() as m: # <-- (...) does not implement __enter__ 
    pm.Normal("a", mu=0, sigma=1)
    trace = pm.sample()

az.plot_posterior(trace)

and before that one, this one has been there for a long time:

# script.py
import pymc as pm
import arviz as az

with pm.Model() as m:
    pm.Normal("a", mu=0, sigma=1)
    trace = pm.sample()

az.plot_posterior(trace) # <-- 9:19 - error: "trace" is possibly unbound (reportUnboundVariable)

This PR implements two small changes:

  1. Sets the type of the instance variable in the ContextMeta __call__ method to "Model". This fixes the first error.
  2. Sets the return type of the context manager used by with pm.Model() to return None, which means that it won't be catching errors.

In the first fix, instance should be a resultant type that the ContextMeta is a metaclass for. I'm not sure how if a type hint that says "returns the child of a metaclass" exists. But I think this is safe, since ContextMeta isn't used as a metaclass anywhere else, and I'm not going for perfect type coverage, just to make it easy for people to code with autocompletion.

Checklist

Bugfixes

  • Fixed pm.Model not being interpreted as a context manager by pyright
  • Fixed unbound type errors due to objects being created inside the pm.Model context

📚 Documentation preview 📚: https://pymc--6809.org.readthedocs.build/en/6809/

@codecov
Copy link

codecov bot commented Jul 2, 2023

Codecov Report

Merging #6809 (ce3831e) into main (f67ff8b) will increase coverage by 0.00%.
The diff coverage is 50.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #6809   +/-   ##
=======================================
  Coverage   92.02%   92.02%           
=======================================
  Files          95       95           
  Lines       16261    16262    +1     
=======================================
+ Hits        14964    14965    +1     
  Misses       1297     1297           
Impacted Files Coverage Δ
pymc/model.py 90.95% <50.00%> (+0.01%) ⬆️

@thomasaarholt
Copy link
Contributor Author

Tagging @michaelosthege on this, since he reviewed my other typing PR. I've also edited the original post in this PR and deleted my now superfluous comments.

I realise it's summer holidays for many, and I'm in no rush to get it merged, but don't want this to get lost.

@ricardoV94
Copy link
Member

Thanks @thomasaarholt!

@michaelosthege michaelosthege merged commit 8e89828 into pymc-devs:main Jul 10, 2023
@thomasaarholt thomasaarholt deleted the thomasaarholt-model-context-type branch July 11, 2023 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants