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

Install import hook earlier during pytest initialization #143

Merged
merged 1 commit into from Aug 14, 2020
Merged

Install import hook earlier during pytest initialization #143

merged 1 commit into from Aug 14, 2020

Commits on Aug 14, 2020

  1. Install import hook earlier during pytest initialization

    The pytest plugin does not work at all in this very common scenario:
    
    - Application/library uses a conftest.py file with pytest configuration
    - This conftest.py imports application/library modules.
    
    Even worse, no matter what is passed as the ‘--typeguard-packages=’
    command line argument, no warnings or errors will be reported at all.
    Typeguard will be fully silent. this this makes it seem the code is
    fully correct, while in reality the code is not checked by typeguard
    *at all*. Oops. 💥 🙈 😱
    
    The problem is that the pytest plugin used the ‘pytest_sessionstart()’
    hook, which triggers only after conftest.py files (or other pytest
    configuration files) have been loaded already. This is too late to
    install the import hook: the targeted modules may have been imported
    already at this point!
    
    This pull request fixes this problem in the following way:
    
    - Use ‘pytest_configure()’ instead of ‘pytest_sessionstart()’, since
      the former triggers before any conftest files are loaded.
    
    - Make it impossible to silently skip checks for packages specified on
      the command line that cannot actually be checked. In those situations,
      raise an error with all the details. Example for
      ‘--typeguard-packages=sys’:
    
      INTERNALERROR> RuntimeError: typeguard cannot check these packages that are already imported: sys
    
    See also:
    
    - https://docs.pytest.org/en/stable/reference.html#pytest.hookspec.pytest_configure
    - https://docs.pytest.org/en/stable/reference.html#pytest.hookspec.pytest_sessionstart
    wbolster committed Aug 14, 2020
    Copy the full SHA
    92f46ee View commit details
    Browse the repository at this point in the history