beartype / pytest-beartype

A pytest plugin to automatically run `beartype` on your test cases.
MIT License
9 stars 0 forks source link

[Feature Request] `pytest.ini` option for configuring `beartype_packages` #1

Closed leycec closed 7 months ago

leycec commented 7 months ago

Thanks so much for all this awesome devtooling, @tusharsadhwani. My eyes have painfully turned into stars. :star_struck:

I have an itsy-bitsy feature request that will surely consume your entire social and work life, destroying quality of sleep and happiness levels for months be possible for a genius-tier intellect such as yours. When you find a free moment over the next several lifetimes, would you mind adding support for a parallel pytest.ini-style configuration item named beartype_packages?

Pretend this makes sense:

# In the top-level "pytest.ini" file for a project:
...

[beartype]
beartype_packages = final_doom,u_wut_mate,strawberry.pancakes  # <-- my favourite packages

In theory, adding the following one-liner i totally lied. it's alot of lines, actually. to src/pytest_beartype/__init__.py should suffice. In practice, my eyeballs are still stars and I can barely see anything:

...
def pytest_addoption(parser: "pytest.Parser") -> None:
    # Add beartype-specific "pytest" options exposed by this plugin.
    group = parser.getgroup("beartype")

    # Help text for the "--beartype-packages" and "beartype_packages" options.
    beartype_packages_help = (
        "comma-delimited list of the fully-qualified names of "
        "all packages and modules to type-check with beartype"
    )

    group.addoption(
        "--beartype-packages",
        action="store",
        help=beartype_packages_help,
    )

    #FIXME: This should do it. Of course, it probably won't. It'll probably break everything.
    #See also relevant upstream docos:
    #    https://docs.pytest.org/en/7.1.x/reference/reference.html#pytest.Parser.addini
    group.addini(
        'beartype_packages',
        help=beartype_packages_help,
        type='args',  # <-- probably? maybe also 'linelist'? i dunno. i defer to your all-seeing eye.
        default='',
    )

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", default='') +
        config.getini("beartype_packages")
    )
    ...

For unknown reasons, OptionGroup doesn't seem to have an addini() method. So the above group.addini() call should probably be parser.addini() instead, I guess? pytest: it just don't make sense.

And... that escalated fast. This was supposed to be easy, pytest. This is why my eyes hurt tonight. It's not just the stinging cold. :star_struck:

tusharsadhwani commented 7 months ago

Sure thing. Let me check.

leycec commented 7 months ago

OMG. You're... sooooo blazing fast. I beg you to also sleep, eat food, and play many video games.

tusharsadhwani commented 7 months ago

So the following will work:

[pytest]
beartype_packages='my_project'

Once the linked PR is merged. I think that's good. (If you use a pyproject.toml anywhere, you could also see if putting [tool.pytest] in that works. I'm an old-school no-toml guy.)

tusharsadhwani commented 7 months ago

Ooh, I missed the suggestion of concatenating the two package lists from ini and option. I'll push that as well.


Update: Should be good now.