Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

299
Vistas
How do I cleanly test equality of objects in Mypy without producing errors?

I have the following function:

import pandas as pd

def eq(left: pd.Timestamp, right: pd.Timestamp) -> bool:
    return left == right

I get the following error when I run it through Mypy:

error: Returning Any from function declared to return "bool"

I believe this is because Mypy doesn't know about pd.Timestamp so treats it as Any. (Using the Mypy reveal_type function shows that Mypy treats left and right as Any.)

What is the correct way to deal with this to stop Mypy complaining?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

you can cast it as a bool.

import pandas as pd

def eq(left: pd.Timestamp, right: pd.Timestamp) -> bool:
    return bool(left == right)

if mypy doesn't like that you can import cast from typing and use that to cast it to a bool.

import pandas as pd
from typing import cast

def eq(left: pd.Timestamp, right: pd.Timestamp) -> bool:
    result = bool(left == right)
    return cast(bool, result)
over 4 years ago · Santiago Trujillo Denunciar

0

I have tested it also without using the cast command. The Mypy check is passed without any error with the following code:

import pandas as pd

def eq(left: pd.Timestamp, right: pd.Timestamp) -> bool:
    return bool(left == right)

(Tested with Mypy 0910.)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda