Skip to content

mypy report custom builtins as not defined #12172

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

Open
svartkanin opened this issue Feb 13, 2022 · 3 comments
Open

mypy report custom builtins as not defined #12172

svartkanin opened this issue Feb 13, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@svartkanin
Copy link

Bug Report

I'm using the package gettext from the standard lib to translate texts inside the application. This will install the function _() in Python’s builtins namespace,
When registering a custom built-in in Pythons namespace then mypy does not recognize that as such and reports it as an unknown.

est.py:6: error: Name "_" is not defined
test.py:19: error: Name "_" is not defined
Found 2 errors in 1 file (checked 1 source file)

To Reproduce

Below is an example code that registers the _ as a bultin-in which can then be used as a standalone function in the code

class Translation:
	def __init__(self, message: str):
		self.message = message

	def __str__(self) -> str:
		translate = _
		if translate is Translation:
			return self.message
		return translate(self.message)

	@classmethod
	def install(cls):
		import builtins
		builtins._ = cls


Translation.install()

s = _('something')
print(s)

Running mypy on this piece of code will report the following:

est.py:6: error: Name "_" is not defined
test.py:19: error: Name "_" is not defined
Found 2 errors in 1 file (checked 1 source file)

Expected Behavior

mypy should not report on custom defined built-ins that have been registered dynamically in the code.
A possible solution could be to do something similar to flake8 which allows to specify a config parameter

[flake8]
builtins = _

Actual Behavior

Reports that the dynamically defined builtins are not defined.

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.2
  • Operating system and version: Arch linux
@svartkanin svartkanin added the bug mypy got something wrong label Feb 13, 2022
@hauntsaninja
Copy link
Collaborator

mypy doesn't have a solution for monkeypatching. You could try adding if TYPE_CHECKING: _: Any or something. Some discussion at #8727

@erictraut
Copy link

We received many similar requests from pyright users. There are a couple of use cases. One is monkey-patching of the builtins namespace (which I agree is a really bad idea and should be discouraged). It's also useful for supporting Python distros that add new symbols to the builtins namespace (which is a more legit use case).

After exploring various options, I decided to add support for a special __builtins__.pyi stub file that users can define locally to their project. If this file is present, pyright adds it as a new scope between modules and the normal builtins.pyi scope. This allows users to easily define a handful of new builtins symbols without copying and modifying the builtins.pyi file (with all the maintenance headaches that implies).

I implemented this feature a couple of weeks ago. So far, it seems to be working really well for pyright users, and it was simple to implement. Not only does it work for type checking, it also works with other language server features like completion suggestions. Here's the documentation with additional details.

If that solution sounds good generally, perhaps mypy and other Python type checkers would like to adopt this same approach? I can see value in standardizing on a solution.

@jokance
Copy link

jokance commented Jun 17, 2022

Any solution for mypy?

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

No branches or pull requests

4 participants