-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Open
Labels
topic: fixturesanything involving fixtures directly or indirectlyanything involving fixtures directly or indirectlytype: bugproblem that needs to be addressedproblem that needs to be addressed
Description
description
I have some fixtures in the same test file, and I need to reload one of them as following:
@pytest.fixture()
def f1():
return 1
@pytest.fixture(name="f1")
def f0():
return 0
what i want
When i call “f1” its return 0 because its actually executed “f0” as following:
class TestLib:
def test_1(self, f1):
assert f1 == 0
pytest result
But this is fail for test
________________________________ TestLib.test_1 ________________________________
self = <lib.test.test_lib.TestLib object at 0x104b3df40>, f1 = 1
def test_1(self, f1):
> assert f1 == 0
E assert 1 == 0
lib/test/test_lib.py:21: AssertionError
=========================== short test summary info ============================
FAILED lib/test/test_lib.py::TestLib::test_1 - assert 1 == 0
env
os:mac 15.0.1
python:3.12.4
pytest:8.3.2
more detail
When I change name ‘f0’ -> 'f2', as following, it works well:
@pytest.fixture()
def f1():
return 1
@pytest.fixture(name="f1")
def f2():
return 2
So I summarized that the overloading of fixtures in the same file is not in the order of bytecode compilation, but in the lexicographic order of functions, resulting in the phenomenon where f0 cannot reload f1 while f2 can reload f1.
I'm not sure if this is a feature or a bug
Metadata
Metadata
Assignees
Labels
topic: fixturesanything involving fixtures directly or indirectlyanything involving fixtures directly or indirectlytype: bugproblem that needs to be addressedproblem that needs to be addressed
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
RonnyPfannschmidt commentedon Nov 12, 2024
We should error on such conflicts
bluetech commentedon Dec 16, 2024
I'd note that doing this might conceivably make sense if the one requests the other, e.g.:
I'm not necessarily saying we shouldn't make this an error, but it's something to consider.
We can also only raise an error only if the fixture doesn't transitively request itself, but that might be difficult.
Correct, this is because we use
dir
(inparsefactories
) which sorts. I actually think this sorting is not good, and we'll be better without it. We should use logic similar to what we have inPyCollector.collect()
.dongfangtianyu commentedon Dec 17, 2024
At first glance, it might seem meaningful, but it feels counterintuitive.
In scenarios where the name
fix
must be retained, and two different implementations (possibly with different scopes or other distinctions) need to coexist, the more common approach looks like this:On the other hand, the following implementation seems overly contrived (especially when both fixtures are in the same file):
According to pytest's current implementation, the unique advantage of this approach is that it creates a fixture that cannot be directly request by any test, It can only be request by another fixture with the same name but declared later.
Just my thoughts:
bluetech commentedon Dec 17, 2024
I definitely agree it feels odd in isolation. However, if we consider that pytest supports to concept of overriding fixtures, the question becomes, should we disallow overriding within a single file (module)?
My feeling on this is that we shouldn't disallow it, mostly because I dislike special cases and I like orthogonality. My feeling would be different if this were a common issue causing confusion, but at least for me, this is the first time I see it come up. And actually the original reporter (@steadyfirmness) seems to dislike only the sorting behavior, not that it's allowed.
WDYT?
BTW @dongfangtianyu, if you feel like it, a PR to remove the sorting behavior of
parsefactories
(replacedir
with code like inPyCollector
) would be great, regardless of the outcome of the discussion above.RonnyPfannschmidt commentedon Dec 17, 2024
Same name in Same file should be conflict unless someone demonstrates a valid use case that's not a thedaylywtf submission or esoteric code golfing
bluetech commentedon Dec 17, 2024
Well, there isn't really a reason for anyone to write this Python file:
yet I still don't think Python should disallow it. The downsides of disallowing is added complexity, and preventing any creative usage of it. The upside is to prevent mistakes, but I don't think it's a common mistake.
steadyfirmness commentedon Dec 17, 2024
Yes, as he(@bluetech ) said, treating test code as a project and solving scalability issues by adding code is the best approach, as shown below.It can better reflect the process of operation:
BTW:
Pytest provides another overload and override mechanism compared to Python, which is a feature of Pytest. I don't want to abandon it, I just want it to become intuitive. The current sorting mode requires additional understanding costs and limits function names.
RonnyPfannschmidt commentedon Dec 17, 2024
Pytest should warn or error on this as it's always a user error
Linters catch function override
They don't catch name parameters
I consider python allows redefiniton a strawman as it's either used for a valid pattern like overrides/properties or it's incorrect code
As far as I'm concerned in same module name conflicts we know something is wrong and we must make it known to the user
dongfangtianyu commentedon Dec 18, 2024
@bluetech cool, your description hits the nail on the head.
As described in the documentation, coverage targets different levels. So the question becomes: should we prohibit repeated coverage at the same level?
If such a requirement does exist, it seems more appropriate to implement it using Python code (if after
parsefactories
can work likePyCollector
).Functions can be used completely before being overridden, same-level fixtures don't seem to be like this, they can only be used by the fixture that overwrites them。