Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertIsNotNone(foo) in tests is not treated the same as assert foo is not None #5088

Closed
timabbott opened this issue May 20, 2018 · 5 comments

Comments

@timabbott
Copy link

As part of Zulip's strict-optional effort, I had to convert a test that was using assertIsNotNone(foo) to use assert foo is not None in order to get mypy to pass. I think mypy should treat those two the same.

@JelleZijlstra
Copy link
Member

Is assertIsNotNone here a custom function? One way to implement this could be to type assertisNotNone as:

@overload
def assertisNotNone(obj: None) -> NoReturn: ...
@overload
def assertIsNotNone(obj: Any) -> None: ...

Haven't tested it though.

@elazarg
Copy link
Contributor

elazarg commented May 20, 2018

@JelleZijlstra that's #4063. It is not implemented.

@timabbott
Copy link
Author

It's part of the standard unittest library: https://docs.python.org/2/library/unittest.html#unittest.TestCase.debug

@ethanhs
Copy link
Collaborator

ethanhs commented May 21, 2018

I think this can be closed in favor of #4063 then.

@1kastner
Copy link

Is assertIsNotNone here a custom function? One way to implement this could be to type assertisNotNone as:

@overload
def assertisNotNone(obj: None) -> NoReturn: ...
@overload
def assertIsNotNone(obj: Any) -> None: ...

Haven't tested it though.

Is that something that should be added to the typeshed description of unittest then? I checked https://github.com/python/typeshed/blob/master/stdlib/unittest/case.pyi#L120 which reads this:

def assertIsNotNone(self, obj: object, msg: Any = ...) -> None: ...

I have seen the forward to #4063. However, that issue is open for 5 years with no result. And currently I prefer to have this issue solved. I just do not want to double-check everything with both an assert statement and a unittest check. I also do not want to refactor all unittests of a project just because of a missing functionality of mypy. I hope that makes it understandable why it might be more reasonable to pick up this track. Thank you!

ThunderKey added a commit to ThunderKey/typeshed_client that referenced this issue Oct 28, 2022
Using assert because mypy does not yet support type guards from unittest
See python/mypy#5088
ThunderKey added a commit to ThunderKey/typeshed_client that referenced this issue Oct 28, 2022
Using assert because mypy does not yet support type guards from unittest
See python/mypy#5088
PeterJCLaw added a commit to PeterJCLaw/srcomp that referenced this issue Mar 26, 2023
mypy doesn't yet understand that `assertIsNotNone` validates the
non-None nature of the variable.

Related issues:
- python/mypy#5088
- python/mypy#4063
- python/typing#930
bdarnell added a commit to bdarnell/tornado that referenced this issue Jun 10, 2024
That PR arrived while our CI was broken and I
manually verified that the tests passed but didn't
run the linters. These changes are running in to
python/mypy#5088
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants