Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 430
Dan Avatar asked Nov 23 '21 13:11

Dan


People also ask

How do I ignore MYPY errors?

You can use a special comment # type: ignore[code, ...] to only ignore errors with a specific error code (or codes) on a particular line. This can be used even if you have not configured mypy to show error codes. Currently it's only possible to disable arbitrary error codes on individual lines using this comment.

What is Mypy_cache?

Incremental mode By default, mypy will store type information into a cache. Mypy will use this information to avoid unnecessary recomputation when it type checks your code again. This can help speed up the type checking process, especially when most parts of your program have not changed since the previous mypy run.

Why does mypy report an error when I try to call none?

(Note that in Python, None is not an empty reference but an object of type None .) In this example mypy will go on to check the last line and report an error, since mypy thinks that the condition could be either True or False: If you use the --warn-unreachable flag, mypy will generate an error about each unreachable code block.

How to check if a string is a program in mypy?

Asks mypy to type check the provided string as a program. A regular expression that matches file names, directory names and paths which mypy should ignore while recursively discovering files to check. Use forward slashes on all platforms. For instance, to avoid discovering any files named setup.py you could pass --exclude '/setup\.py$'.

Does mypy use strict optional checking?

Starting from mypy 0.600, mypy uses strict optional checking by default, and the None value is not compatible with non-optional types. It’s easy to switch back to the older behavior where None was compatible with arbitrary types (see Disabling strict optional checking ).

How to prevent comparisons like 42 == 'No' in mypy?

By default, mypy allows always-false comparisons like 42 == 'no' . Use this flag to prohibit such comparisons of non-overlapping types, and similar identity and container checks:


2 Answers

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)
like image 51
Ollie in PGH Avatar answered Sep 24 '22 03:09

Ollie in PGH


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.)

like image 39
Eyal Asulin Avatar answered Sep 25 '22 03:09

Eyal Asulin