Skip to content
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

mypy complaining when using literals #584

Open
ntadej opened this issue Apr 4, 2024 · 9 comments
Open

mypy complaining when using literals #584

ntadej opened this issue Apr 4, 2024 · 9 comments

Comments

@ntadej
Copy link

ntadej commented Apr 4, 2024

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
Copy link
Member

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
Copy link
Member

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
Copy link
Author

ntadej commented Apr 4, 2024

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
Copy link
Member

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
Copy link
Member

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

@henryiii
Copy link
Member

henryiii commented Apr 4, 2024

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
Copy link
Author

ntadej commented Apr 4, 2024

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
Copy link
Member

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
Copy link
Member

henryiii commented Apr 5, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants