scikit-hep / particle

Package to deal with particles, the PDG particle data table, PDGIDs, etc.
BSD 3-Clause "New" or "Revised" License
149 stars 25 forks source link

mypy complaining when using literals #584

Open ntadej opened 5 months ago

ntadej commented 5 months ago

I use the following code:

from particle.pdgid import literals as pdgid
foo = pdgid.gamma.abspid

mypy complains with

Module has no attribute "gamma"

Is there a workaround?

eduardo-rodrigues commented 5 months ago

Hi. Thank you for the interest in the package.

Why do you not follow the examples in the README, so something like

In [1]: from particle.pdgid import literals as lid

In [2]: lid.gamma Out[2]: <PDGID: 22>

In [3]: lid.gamma.abspid Out[3]: 22

:)?

It is in general a bad idea to import as the name of something common/in the package/...

eduardo-rodrigues commented 5 months ago

This being said, you mention MyPy, so what exactly are you doing, and for what? In any case I hope the examples/notebooks are a good source to get started. Let us know, thank you.

ntadej commented 5 months ago

The issue is still the same if I use the lid alias.

I just type my code. I suspect particles are lazy-loaded and it confuses the type checker.

eduardo-rodrigues commented 5 months ago

I'm sorry but I'm still failing to understand in which env you get this. Isn't something you can switch off for usage, anyway?

eduardo-rodrigues commented 5 months ago

Let me just ping @henryiii in case he has some insight. Else I would be inclined to close this issue.

henryiii commented 5 months ago

It’s because we take a dynamic approach to filling these out. It’s not something that changes frequently, so personally I’d be fine with figuring out a more static-friendly way to do it.

ntadej commented 5 months ago

It is not super urgent! For now I just ignore a few lines where this code is used. Will investigate a bit more if there is a simple workaround and report back if I find anything.

eduardo-rodrigues commented 5 months ago

The dynamic approach is nice since we did the job "years ago" and got it all behind our backs ever since, as it were.

I'm afraid I still do not see the way you are working to have the "issue", and you're certainly the first one to mention it in 2-3 years. Would adding a pragma ignore somewhere help?

henryiii commented 5 months ago

I've talked about this before for several years, I've been bothered by the fact that we have a static list of things and we can't use it statically in type checkers because we are dynamically generating it. pdgid.gamma should be statically available, we aren't reading it from somewhere.

There are a few options for fixing it, such as generating the file instead of dynamically generating the entries, or writing out a .pyi file.