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

294
Views
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 answers
Answer question

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 Report

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 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!