Commit Graph

197 Commits

Author SHA1 Message Date
pre-commit-ci[bot] c0532dda18
[pre-commit.ci] pre-commit autoupdate (#12115)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-03-13 15:30:18 +02:00
Ran Benita 42785cca44 Revert "testing: temporarily disable test due to hypothesis issue (#11836)"
This reverts commit 5cd0535395.
2024-02-07 22:13:23 +02:00
Pierre Sassoulas 4588653b24 Migrate from autoflake, black, isort, pyupgrade, flake8 and pydocstyle, to ruff
ruff is faster and handle everything we had prior.

isort configuration done based on the indication from
https://github.com/astral-sh/ruff/issues/4670, previousely based on
reorder-python-import (#11896)

flake8-docstrings was a wrapper around pydocstyle (now archived) that
explicitly asks to use ruff in https://github.com/PyCQA/pydocstyle/pull/658.

flake8-typing-import is useful mainly for project that support python 3.7
and the one useful check will be implemented in https://github.com/astral-sh/ruff/issues/2302

We need to keep blacken-doc because ruff does not handle detection
of python code inside .md and .rst. The direct link to the repo is
now used to avoid a redirection.

Manual fixes:
- Lines that became too long
- % formatting that was not done automatically
- type: ignore that were moved around
- noqa of hard to fix issues (UP031 generally)
- fmt: off and fmt: on that is not really identical
  between black and ruff
- autofix re-order in pre-commit from faster to slower

Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-02-02 09:27:00 +01:00
Bruno Oliveira 878af85aef
mypy: disallow untyped defs by default (#11862)
Change our mypy configuration to disallow untyped defs by default, which ensures *new* files added to the code base are fully typed.

To avoid having to type-annotate everything now, add `# mypy: allow-untyped-defs` to files which are not fully type annotated yet.

As we fully type annotate those modules, we can then just remove that directive from the top.
2024-01-28 10:12:42 -03:00
Ran Benita 5cd0535395
testing: temporarily disable test due to hypothesis issue (#11836)
Ref: https://github.com/pytest-dev/pytest/pull/11825#issuecomment-1894094641
2024-01-17 22:53:04 +02:00
Ran Benita 385796ba49 Rework Session and Package collection
Fix #7777.
2023-12-10 17:01:39 +02:00
Bruno Oliveira 7156a97f9a Fix crash using empty string for parametrized value more than once
Fixes #11563.
2023-10-27 17:26:12 -03:00
ryanpudd 3ab70cd561
Use hyphenated cmdline options in docs (#11490)
Fix #11091
2023-10-10 21:16:24 +00:00
Sadra Barikbin 09b78737a5
Move `fixtures.py::add_funcarg_pseudo_fixture_def` to `Metafunc.parametrize` (#11220)
To remove fixtures.py::add_funcargs_pseudo_fixture_def and add its logic
i.e. registering funcargs as params and making corresponding fixturedefs,
right to Metafunc.parametrize in which parametrization takes place.

To remove funcargs from metafunc attributes as we populate metafunc
params and make pseudo fixturedefs simultaneously and there's no need to
keep funcargs separately.
2023-08-09 17:13:45 +00:00
Sadra Barikbin e8a8a5f320
python: fix scope assignment for indirect parameter sets (#11277)
Previously, when assigning a scope for a fully-indirect parameter set,
when there are multiple fixturedefs for a param (i.e. same-name fixture
chain), the highest scope was used, but it should be the lowest scope,
since that's the effective scope of the fixture.
2023-08-06 13:59:54 +00:00
Ran Benita 71e627aa8f fixtures: fix crash when `parametrize(scope="package")` is used without a Package
There as handling for `scope="class"` without a class, but not for
`scope="package"` without a package. It would fail the assert.
2023-07-28 16:13:02 +03:00
Zac Hatfield-Dodds f4e3b4ad98 Drop Python 3.7 2023-06-30 14:55:42 -07:00
nondescryptid a704605cf1 Fix encoding warnings 2023-06-20 04:55:39 -07:00
Ran Benita 310b67b227
Drop attrs dependency, use dataclasses instead (#10669)
Since pytest now requires Python>=3.7, we can use the stdlib attrs
clone, dataclasses, instead of the OG package.

attrs is still somewhat nicer than dataclasses and has some extra
functionality, but for pytest usage there's not really a justification
IMO to impose the extra dependency on users when a standard alternative
exists.
2023-01-20 11:13:36 +02:00
Ran Benita c3aa4647c7 python: unify code to generate ID from value
In the following

    @pytest.mark.parametrize(..., ids=[val])

the ID values are only allowed to be `str`, `float`, `int` or `bool`.

In the following

    @pytest.mark.parametrize(..., [val])

    @pytest.mark.parametrize(..., [pytest.param(..., id=val])

a different code path is used, which also allows `bytes`, `complex`,
`re.Pattern`, `Enum` and anything with a `__name__`.

In the interest of consistency, use the latter code path for all cases.
2022-02-12 19:22:37 +02:00
Tobias Deiminger b21b008118 Refactor idmaker functions into class IdMaker
This commit only refactors, it does not change or add functionality yet. Public
API is retained. Reason or refactoring:

User provided parameter IDs (e.g. Metafunc.parametrize(ids=...)) had so far
only been used to calculate a unique test ID for each test invocation. That
test ID was a joined string where each parameter contributed some partial ID.

We're soon going to reuse functionality to generate parameter keys for
reorder_items and FixtureDef cache. We will be interested in the partial
IDs, and only if they originate from explicit user information. Refactoring
makes logic and data accessible for reuse, and increases cohesion in general.
2022-01-26 15:42:34 +02:00
Bruno Oliveira a83b359cf0
Refactor internal scope handling by introducing Scope enum
PR #8913
2021-08-01 06:11:56 -03:00
Graeme Smecher 5c04674e96 Generate useful parameterization IDs for complex() numbers. 2021-07-12 12:27:24 -07:00
Miro Hrončok 5165bf97c6
Revert "Adjust enum reprs for Python 3.10" (#8896)
This reverts commit 710446420c.

The change was reverted in Python 3.10.0b4:

https://mail.python.org/archives/list/python-dev@python.org/message/LSTMFAPSPD3BGZ4D6HQFODXZVB3PLYKF/
2021-07-12 14:32:27 +00:00
Florian Bruhin 710446420c Adjust enum reprs for Python 3.10
Potential fix for #8546
2021-05-04 17:24:50 +02:00
Miroslav Šedivý fbe66244b8
Fix some typos, remove redundant words and escapes (#8564)
* doc: Fix typos, remove double words

* Remove redundant escapes in regex
2021-04-19 20:39:08 +00:00
pre-commit-ci[bot] ee03e31831
[pre-commit.ci] pre-commit autoupdate (#8201)
* [pre-commit.ci] pre-commit autoupdate

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* manual fixes after configuration update

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Anthony Sottile <asottile@umich.edu>
2020-12-30 11:56:09 +02:00
Ran Benita bd76042344 python: export pytest.Metafunc for typing purposes
The type cannot be constructed directly, but is exported for use in type
annotations, since it is reachable through existing public API.
2020-12-26 21:05:02 +02:00
antonblr 4ed9a38519 tests: Migrate testing/python to pytester fixture 2020-12-13 07:42:12 -08:00
Hugo Martins 5b2e5e8a40
Improve summary stats when using '--collect-only' (#7875)
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2020-11-08 11:45:10 -03:00
Anthony Sottile 33d119f71a py36+: com2ann 2020-10-05 18:33:17 -07:00
Ran Benita a267a622eb python: fix empty parametrize() leading to "NotSetType.token" id
In ff8b7884e8 NOTSET was changed to a
singleton enum, which ended up unexpectedly triggering a code path in ID
generation which checks for `isinstance(Enum)`.

Add an explicit case for it, which is not too bad anyway.
2020-08-25 22:01:43 +03:00
Ran Benita b8471aa527 testing: fix some docstring issues
In preparation for enforcing some docstring lints.
2020-08-03 10:10:43 +03:00
Ran Benita be656dd4e4 typing: set disallow_any_generics
This prevents referring to a generic type without filling in its generic
type parameters.

The FixtureDef typing might need some more refining in the future.
2020-08-01 20:39:15 +03:00
Ran Benita 8d98de8f8a typing: set no_implicit_reexport
In Python, if module A defines a name `name`, and module B does `import
name from A`, then another module C can `import name from B`.

Sometimes it is intentional -- module B is meant to "reexport" `name`.
But sometimes it is just confusion/inconsistency on where `name` should
be imported from.

mypy has a flag `--no-implicit-reexport` which puts some order into
this. A name can only be imported from a module if

1. The module defines the name
2. The module's `__all__` includes the name
3. The module imports the name as `from ... import .. as name`.

This flag is included in mypy's `--strict` flag.

I like this flag, but I realize it is a bit controversial, and in
particular item 3 above is a bit unfriendly to contributors who don't
know about it. So I didn't intend to add it to pytest.

But while investigating issue 7589 I came upon mypy issue 8754 which
causes `--no-implicit-reexport` to leak into installed libraries and
causes some unexpected typing differences *in pytest* if the user uses
this flag.

Since the diff mostly makes sense, let's just conform to it.
2020-07-31 10:09:11 +03:00
Ran Benita a2f021b6f3 Remove no longer needed `noqa: F821` uses
Not needed since pyflakes 2.2.0.
2020-07-10 13:08:56 +03:00
Ran Benita e079ebbd57 python: more type annotations 2020-07-10 13:08:56 +03:00
Ran Benita e492b1d567 python: don't pass entire Item for generating ID
Just the nodeid is enough for the error messages.
This removes an import cycle.
2020-06-30 13:13:45 +03:00
Ran Benita ef34729541 Type annotate fixtures.py & related 2020-06-05 11:34:19 +03:00
Bruno Oliveira 010e711971 Merge pull request #6914 from nicoddemus/revert-6330
Revert "[parametrize] enforce explicit argnames declaration (#6330)"
2020-03-13 10:49:48 -03:00
Ran Benita 24dcc76495 Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.

Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:

    def pytest_configure(config):
        if config.option.runxfail:
            # yay a hack
            import pytest

            old = pytest.xfail
            config._cleanup.append(lambda: setattr(pytest, "xfail", old))

            def nop(*args, **kwargs):
                pass

            nop.Exception = xfail.Exception
            setattr(pytest, "xfail", nop)
        ...

So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.

`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.

Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-18 23:17:27 +02:00
Ran Benita 5945c3fe88 Type annotate Metafunc 2020-02-15 17:13:18 +02:00
Vladyslav Rachek 9e262038c8
[parametrize] enforce explicit argnames declaration (#6330)
Every argname used in `parametrize` either must
be declared explicitly in the python test function, or via
`indirect` list

Fix #5712
2020-02-06 20:20:25 -03:00
Bruno Oliveira 93b74d28d2 Merge remote-tracking branch 'upstream/master' into mm
Conflicts:
 * 	src/_pytest/_code/code.py
 * 	src/_pytest/main.py
 * 	testing/python/metafunc.py
 * 	testing/test_parseopt.py
 * 	testing/test_pytester.py
2020-01-22 11:03:45 -03:00
Daniel Hahler 5049e25a6a tests: cleanup unused fixtures 2020-01-16 21:12:48 +01:00
Ronny Pfannschmidt c99c7d0f95 deprecate direct node construction and introduce Node.from_parent 2019-11-23 21:54:11 +01:00
Daniel Hahler 2c941b5d13 parametrized: ids: support generator/iterator
Fixes https://github.com/pytest-dev/pytest/issues/759

- Adjust test_parametrized_ids_invalid_type, create list to convert tuples
  Ref: https://github.com/pytest-dev/pytest/issues/1857#issuecomment-552922498

- Changelog for int to str conversion
  Ref: https://github.com/pytest-dev/pytest/issues/1857#issuecomment-552932952
2019-11-20 19:02:17 +01:00
Daniel Hahler ccb3ef3b33 testing/python/metafunc.py: import _idval once 2019-11-20 18:02:30 +01:00
Daniel Hahler 4ad61cbcf6 Improve check for misspelling of parametrize
- there is no need to do this with `--strict-markers`
- it can be done when looking up marks, instead of for every generated
  test
2019-11-19 16:05:52 +01:00
Daniel Hahler 2ad2fbc9a2 Metafunc: remove hack for DefinitionMock
Done initially in 99015bfc8.
2019-11-18 18:19:34 +01:00
Anthony Sottile cc78444c30 Merge remote-tracking branch 'origin/master' into mm 2019-11-15 15:26:57 -08:00
Bruno Oliveira 6f2c0fd2e8 Show a better message when 'request' is used in parametrize
Fix #6183
2019-11-13 19:57:10 -03:00
Daniel Hahler fc1c015c6b tests: remove test_nested_marks (xfail)
It currently fails with a TypeError, and was not updated since 2013 -
therefore it can be assumed that it is not important to support it.

```
____________________ ERROR collecting test_nested_marks.py _____________________
…/Vcs/pluggy/src/pluggy/hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
…/Vcs/pluggy/src/pluggy/manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
…/Vcs/pluggy/src/pluggy/manager.py:337: in traced_hookexec
    return outcome.get_result()
…/Vcs/pluggy/src/pluggy/manager.py:335: in <lambda>
    outcome = _Result.from_call(lambda: oldcall(hook, hook_impls, kwargs))
…/Vcs/pluggy/src/pluggy/manager.py:87: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
…/Vcs/pytest/src/_pytest/python.py:235: in pytest_pycollect_makeitem
    res = list(collector._genfunctions(name, obj))
…/Vcs/pytest/src/_pytest/python.py:404: in _genfunctions
    self.ihook.pytest_generate_tests.call_extra(methods, dict(metafunc=metafunc))
…/Vcs/pluggy/src/pluggy/hooks.py:324: in call_extra
    return self(**kwargs)
…/Vcs/pluggy/src/pluggy/hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
…/Vcs/pluggy/src/pluggy/manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
…/Vcs/pluggy/src/pluggy/manager.py:337: in traced_hookexec
    return outcome.get_result()
…/Vcs/pluggy/src/pluggy/manager.py:335: in <lambda>
    outcome = _Result.from_call(lambda: oldcall(hook, hook_impls, kwargs))
…/Vcs/pluggy/src/pluggy/manager.py:87: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
…/Vcs/pytest/src/_pytest/python.py:130: in pytest_generate_tests
    metafunc.parametrize(*marker.args, **marker.kwargs)
…/Vcs/pytest/src/_pytest/python.py:965: in parametrize
    function_definition=self.definition,
…/Vcs/pytest/src/_pytest/mark/structures.py:111: in _for_parametrize
    if len(param.values) != len(argnames):
E   TypeError: object of type 'MarkDecorator' has no len()
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
```
2019-11-12 14:44:03 +01:00
Patrick Harmon abc890079f typos 2019-10-31 23:19:35 -05:00
Ronny Pfannschmidt 2c071a060e refactor resolve_arg_value_types
* more explicit type checks
* expand from list+tuple to sequence
2019-07-13 08:13:46 +02:00