Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
¿Por qué mypy no entenderá la instanciación de este objeto?

Estoy tratando de definir una clase que tome otra clase como atributo _model y creará instancias de objetos de esa clase.

 from abc import ABC from typing import Generic, TypeVar, Any, ClassVar, Type Item = TypeVar("Item", bound=Any) class SomeClass(Generic[Item], ABC): _model: ClassVar[Type[Item]] def _compose_item(self, **attrs: Any) -> Item: return self._model(**attrs)

Creo que debería ser obvio que self._model(**attrs) devuelve una instancia de Item , ya que _model se declara explícitamente como Type[Item] y attrs se declara como Dict[str, Any] .

Pero lo que obtengo de mypy 0.910 es:

 test.py: note: In member "_compose_item" of class "SomeClass": test.py:11: error: Returning Any from function declared to return "Item" return self._model(**attrs) ^

¿Qué estoy haciendo mal?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

MyPy a veces puede ser un poco divertido sobre los tipos de clases. Puede resolver esto especificando _model como Callable[..., Item] (que, después de todo, no es una mentira ) en lugar de Type[Item] :

 from abc import ABC from typing import Generic, TypeVar, Any, ClassVar, Callable Item = TypeVar("Item") class SomeClass(Generic[Item], ABC): _model: ClassVar[Callable[..., Item]] def _compose_item(self, **attrs: Any) -> Item: return self._model(**attrs)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!