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

Add support for pytest.ini config #2

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions src/pytest_beartype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@

def pytest_addoption(parser: "pytest.Parser") -> None:
# Add beartype-specific "pytest" options exposed by this plugin.
group = parser.getgroup("beartype")
group.addoption(
"--beartype-packages",
action="store",
help=(
"comma-delimited list of the fully-qualified names of "
"all packages and modules to type-check with beartype"
),
help_msg = (
"comma-delimited list of the fully-qualified names of "
"all packages and modules to type-check with beartype"
)

group = parser.getgroup("beartype")
group.addoption("--beartype-packages", action="store", help=help_msg)
parser.addini("beartype_packages", type="args", help=help_msg)


def pytest_configure(config: "pytest.Config") -> None:
# Comma-delimited string listing the fully-qualified names of *ALL* packages
# and modules to type-check with beartype, corresponding to the
# "--beartype-packages" option defined above by the pytest_addoption() hook.
package_names_str = config.getoption("beartype_packages")
# "beartype_packages" section in the user-defined "pytest.ini" file, or the
# "--beartype-packages" options, defined above by the pytest_addoption() hook.
package_names = config.getini("beartype_packages")

package_names_arg_str = config.getoption("beartype_packages", "")
if package_names_arg_str:
package_names += package_names_arg_str.split(",")

# If the user passed this option...
if package_names_str:
if package_names:
# Defer hook-specific imports. To improve "pytest" startup performance,
# avoid performing *ANY* imports unless the user actually passed the
# "--beartype-packages" option declared by this plugin.
Expand All @@ -43,12 +47,6 @@ class BeartypePytestWarning(BeartypeWarning):
under the active Python interpreter.
"""

# Tuple of the fully-qualified names of these packages and modules.
package_names = tuple(
package_name_str.strip()
for package_name_str in package_names_str.split(",")
)

# Tuple of the subset of these names corresponding to previously
# imported packages and modules under the active Python interpreter.
package_imported_names = tuple(
Expand Down
Loading