Skip to content

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

Closed

Description

@timabbott

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.

Activity

  1. JelleZijlstra commented on May 20, 2018

    @JelleZijlstra
    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.

  2. elazarg commented on May 20, 2018

    @elazarg
    Contributor

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

  3. timabbott commented on May 21, 2018

    @timabbott
    Author
  4. emmatyping commented on May 21, 2018

    @emmatyping
    Member

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

  5. 1kastner commented on Aug 21, 2022

    @1kastner

    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!

  6. added a commit that references this issue on Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions