-
Notifications
You must be signed in to change notification settings - Fork 767
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
How do I get pylance to recognize the _ ("underscore") function from gettext. #1383
Comments
Are you just seeing that If you define an # stub contents ...
__all__ = ['_', 'coolstub'] |
Unfortunately, it does not. |
This seems more like our analysis is making some assumptions about |
Alright, I think I understand the issue better. |
I guess the thing I'm confused about is how you were using a stub to add things into a global namespace; as far as I'm aware, the only ways to do so are:
You can't have |
I'm sorry, I wasn't truly adding things to the global namespace. |
It's not really a good option to fork typeshed; how are the symbols appearing? Are they imported from somewhere, or are they getting injected by some wrapper? |
It is showing up as such: inside the '${workspace}/typings' folder I have an
I had named it Before, It was showing up on autocomplete with a blue icon (little box within brackets). Can't reproduce that anymore. |
Yeah, you don't want to have an Having the the |
I see that the "waiting for user response" label was added to this ticket, but I'm not sure what more information I could provide. From what I got from our discussion, the usage scenario was explained and understood, but it is not yet achievable with Pylance. I'm eager to provide any additional information that is required. |
No, that was done in error; it's back to being marked as needing investigation. |
I think I'm also hit by that bug. I'm not sure because it went quite fast to workarounds in In my case I simply use gettext and its Here's my project: https://github.com/MightyCreak/diffuse |
It appears that the
This is highly suspect and inadvisable. Libraries shouldn't be overwriting the namespace of any other module, especially stdlib modules and most especially the The good news is that this library doesn't automatically do this namespace modification upon initialization. You will get this undesirable behavior only if you call My recommendation is that you replace the call to import gettext
translator = gettext.translation('diffuse', LOCALEDIR)
_ = translator.gettext You could add this code to each file that uses the You might also want to consider giving the symbol a more appropriate name since |
I understand the arguments, but AFAIK (and I might be wrong), gettext is a well-known and widely-used lib, and using |
In flake8 we're able to specify
to add |
For sure that injecting functions into the builtin namespace isn't "good practice", but it's a very common pattern for translation libraries, in one way or another, for several languages. The suggested alternative of manually declaring or importing _ wherever it's used is highly undesirable, because:
IMO, what we need is a mechanism for us to tell pylance that such and such is in the builtin namespace. I can't see any other reasonable way of supporting gettext users. |
I investigated various ways we could support this. The solution I landed on was a special type stub file |
Just to check if I understand the format right, the contents of def _(__msg: str) -> str: ... right? |
Yes, that's the correct syntax. I don't know if that's the right signature for your use case. You could optionally use a positional-only separator rather than using the old-style "double underscore" convention. def _(msg: str, /) -> str: ... |
Thank you so much, Eric! I'll give a spin as soon as the next version comes out. |
This issue has been fixed in version 2022.2.1, which we've just released. You can find the changelog here: CHANGELOG.md |
Hello, all
I work on a project that uses GNU gettext for translating strings. I tried to use the "typings" functionality to get it to recognize the "_" function as follows:
coolstub(...)
is recognized and available globally. Is there a way to make pylance recognize_(...)
as well?Thanks in advance :)
The text was updated successfully, but these errors were encountered: