python / python/mypy

mypy report custom builtins as not defined

Offen
#12,172 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der bereitgestellten Translation.install()-Reproduktion sowie den Details zum mypy-Befehl und zur Konfiguration im Bericht. Ermittle, wie dynamisch zugewiesene builtins behandelt werden und ob eine konfigurationsbasierte Lösung im Umfang liegt; als erledigt gilt die Aufgabe, wenn die Reproduktion _ nicht mehr als nicht definiert meldet, während tatsächlich nicht definierte Namen weiterhin diagnostiziert werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.