-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
0.920: Calling function named "_" is not allowed #11774
Comments
Note: the problem didn't occur until v0.920. |
I'm using an # scratch.py
from __future__ import annotations
import dataclasses
from typing import Any
def _(model_name: str | None, optional: bool = False) -> Any:
return dataclasses.field(metadata=dict(model_name=model_name, optional=optional))
@dataclasses.dataclass
class Book:
title: str = _('book_title')
author: str = _('primary_author')
publisher: str = _('publisher')
quantity: int = _(None, True)
price: float = _('msrp') Running mypy on the above sample produces the following output:
Now I can eliminate the error by renaming the function and then assign it to # scratch.py
...
def field(model_name: str | None, optional: bool = False) -> Any:
return dataclasses.field(metadata=dict(model_name=model_name, optional=optional))
_ = field
...
But why should mypy prevent users from directly defining a function named |
+1 on this issue, I read the original PR but to be honest, I don't understand why calling a function named |
Just for the record: 7edcead is the guilty commit which introduced the regression. We have the same problem in our SW, we have more than 2k call sites for I propose to simply revert the above commit, it seems fundamentally wrong to rule out some commonly used function name in an ad hoc manner. And to be honest: I don't fully understand what the commit is supposed to "fix". |
Some context: this commit was a part of our "better @some.register
def _(arg: int) -> ...
@some.register
def _(arg: str) -> ... But, back then I am +1 on allowing |
Yes, we should allow calling |
Yeah, this is correct. Something like this: def _(arg: str) -> int: ...
def _(arg: int) -> str: ...
_('a') is probably a bug for the same reason that redefining a non-underscore function is a bug. Maybe the fix for this can be allowing calling underscore functions only when they haven't being redefined. None of the examples in this PR redefine
I think the commit that introduced this was ed09f8d. The commit you're talking about added similar support for mypyc (which will probably have to be updated based on whatever the solution to this issue is).
The problem with reverting that commit is that it would be a regression for projects that use |
As a quick fix for the upcoming release, we should probably just allow calling |
I don't really care about the details of how this is going to be fixed, the main point is: The most straightforward use case, i.e. a plain old function/method (no redefinition, decoration, etc.) called def _() -> None: pass
_() should work. What I still don't fully understand: Why should |
Fixes #11774. The issue has relevant background information in the discussion. It may be worth it to eventually add back some restrictions, but we'd only focus the restrictions on singledispatch functions. This is a quick fix to work around a regression.
Fixes #11774. The issue has relevant background information in the discussion. It may be worth it to eventually add back some restrictions, but we'd only focus the restrictions on singledispatch functions. This is a quick fix to work around a regression.
Fixes #11774. The issue has relevant background information in the discussion. It may be worth it to eventually add back some restrictions, but we'd only focus the restrictions on singledispatch functions. This is a quick fix to work around a regression.
Fixes #11774. The issue has relevant background information in the discussion. It may be worth it to eventually add back some restrictions, but we'd only focus the restrictions on singledispatch functions. This is a quick fix to work around a regression.
@JukkaL thanks a lot for prompt fixing!! |
Fixes python#11774. The issue has relevant background information in the discussion. It may be worth it to eventually add back some restrictions, but we'd only focus the restrictions on singledispatch functions. This is a quick fix to work around a regression.
The following code:
produces the following output:
Some lines of code in the test are commented as test would now work without proper
gettext
configuration.Those lines show how a
gettext
-using code typically looks._
is a standard function name forgettext
, created bytranslation().install()
, it should be usable correctly.If the error is really reasonable, at least there should be an option to turn it off.
The text was updated successfully, but these errors were encountered: