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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 20 additions & 5 deletions typeguard/pytest_plugin.py
@@ -1,3 +1,5 @@
import sys

from typeguard.importhook import install_import_hook


Expand All @@ -8,8 +10,21 @@ def pytest_addoption(parser):
'type checking')


def pytest_sessionstart(session):
packages = session.config.getoption('typeguard_packages')
if packages:
package_list = [pkg.strip() for pkg in packages.split(',')]
install_import_hook(packages=package_list)
def pytest_configure(config):
value = config.getoption("typeguard_packages")
if not value:
return

packages = [pkg.strip() for pkg in value.split(",")]

already_imported_packages = sorted(
package for package in packages if package in sys.modules
)
if already_imported_packages:
message = (
"typeguard cannot check these packages because they "
"are already imported: {}"
)
raise RuntimeError(message.format(", ".join(already_imported_packages)))

install_import_hook(packages=packages)