PyHamcrest icon indicating copy to clipboard operation
PyHamcrest copied to clipboard

Package doesn't seem to be compatible with strict mypy

Open lindycoder opened this issue 3 years ago • 2 comments

Hello!

Always been a great fan of this library!

Disclaimer: I'm new to mypy, so this might just be me lacking understadings, if so, i'm sorry i'll close this.

I'm trying to introduce mypy strict=true to my test suite and notice 2 things:

  1. The documented way of importing does seem to fail on mypy.

    from hamcrest import *
    
    def test() -> None:
        assert_that(1, is_(1))
    

    Results in

    error: Name "assert_that" is not defined.
    

    note: vscode also does not recognize import *... unless i miss an extension other than python

  2. If imported manually, there seems to be an issue with dynamically created __all__.

    from hamcrest import assert_that, is_
    
    def test() -> None:
        assert_that(1, is_(1))
    

    Results in

    Module "hamcrest" does not explicitly export attribute "assert_that"; implicit reexport disabled
    

    note: type checking works fine with no_implicit_reexport=false: if you don't pass a matcher to assert_that you get an error.

So i'm not sure what to make of this...

mitigation

  • Setting no_implicit_reexport=false obviously solves the problem... but it would be better to keep it for other repos. Unless there's a way to specify this only for pyhamcrest
  • Using # type: ignore on the from hamcrest import assert_that, is_ line disable the warning along with type checking

reproduce

Here's a repo with 3 files that produce error, reproduce steps are in the readme https://github.com/lindycoder/pyhamcrest-mypy-issue

Thank you very much!

lindycoder avatar Dec 23 '21 18:12 lindycoder

Strict mode is new to me, I must say, but I'll take a look at this when I get the chance.

The import * thing is easily fixed in a way, since that's not the best way of importing in the first place!

brunns avatar Mar 03 '22 06:03 brunns

@lindycoder can you check with 2.0.4 please? I believe we've fixed this in the latest release, but I would like to be sure.

offbyone avatar Aug 13 '22 12:08 offbyone

@offbyone Hi, it does seem to be working with latest mypy, thank you!

lindycoder avatar Aug 29 '22 12:08 lindycoder