Skip to content

Commit

Permalink
Merge pull request #2 from beartype/ini-options
Browse files Browse the repository at this point in the history
Add support for `pytest.ini` config
  • Loading branch information
tusharsadhwani authored Jan 25, 2024
2 parents 10ecfd1 + fa4e9a8 commit cc7922e
Showing 1 changed file with 15 additions and 17 deletions.
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

0 comments on commit cc7922e

Please sign in to comment.