python / python/mypy

mypy report custom builtins as not defined

Aperta
#12,172 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione fornita di Translation.install() e con i dettagli sul comando mypy e sulla configurazione riportati nel report. Determina come vengono gestiti i builtins assegnati dinamicamente e se una soluzione basata sulla configurazione rientra nell'ambito; il lavoro è completato quando la riproduzione non segnala più _ come non definito, mentre i nomi realmente non definiti continuano a essere diagnosticati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.