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

AttributeError: 'Testdir' object has no attribute 'config' #8282

Closed
hroncok opened this issue Jan 26, 2021 · 4 comments
Closed

AttributeError: 'Testdir' object has no attribute 'config' #8282

hroncok opened this issue Jan 26, 2021 · 4 comments

Comments

@hroncok
Copy link
Member

hroncok commented Jan 26, 2021

Hello. I've seen a problem when running tests of pytest-ordering.

The tests fails with:

E       AttributeError: 'Testdir' object has no attribute 'config'

It worked with pytest 6.0.2 but fails with 6.2.2. I cannot match a relevant changelog entry, and there was no deprecation warning in 6.0.

Reproducer:

$ git clone https://github.com/ftobia/pytest-ordering.git
$ cd pytest-ordering
$ tox -e py39
...
    def _item_names_for(tests_content):
        # some strange code to extract sorted items
        items = testdir.getitems(tests_content)
>       hook = testdir.config.hook
E       AttributeError: 'Testdir' object has no attribute 'config'
...
$ .tox/py39/bin/pip list
Package         Version
--------------- -------
attrs           20.3.0
coverage        5.4
iniconfig       1.1.1
packaging       20.8
pip             20.3.3
pluggy          0.13.1
py              1.10.0
pyparsing       2.4.7
pytest          6.2.2
pytest-cov      2.11.1
pytest-ordering 0.6
setuptools      51.0.0
toml            0.10.2
wheel           0.36.2

This is on Linux, Fedora 33 and 34.

Is this a deliberate removal or a regression when Testdir become a wrapper around Pytester?

@nicoddemus
Copy link
Member

Hi @hroncok!

It is "kinda" of a regression, albeit one can argue that API is a very subtle/unintentional one:

pytest/src/_pytest/pytester.py

Lines 1142 to 1143 in 09b1d7c

self.config = config = self.parseconfigure(path, *configargs)
return self.getnode(config, path)

testdir.config would only be set if one calls getmodulecol (which getitems does); before that, it would raise an AttributeError. Not sure this was really ever meant to be public, and arguably a bad API.

We can easily bring it back by using a read-only property to Testdir that returns Optional[Config], but I would prefer to avoid bringing back this kind of "accidental API" and now having to maintain it.

Perhaps we can change pytest-ordering instead? 🤔

@hroncok
Copy link
Member Author

hroncok commented Jan 26, 2021

Hi @nicoddemus!

Sure, we can fix pytest-ordering instead (however it appears quite silent recently, with active forks etc.), but I was not sure if this was intentional or not, that's why I asked first.

The code is:

https://github.com/ftobia/pytest-ordering/blob/492697ee26633cc31d329c1ceaa468375ee8ee9c/tests/test_ordering.py#L15

@pytest.fixture
def item_names_for(testdir):

    def _item_names_for(tests_content):
        # some strange code to extract sorted items
        items = testdir.getitems(tests_content)
        hook = testdir.config.hook
        hook.pytest_collection_modifyitems(session=items[0].session,
                                           config=testdir.config, items=items)
        return [item.name for item in items]

    return _item_names_for

What is the actual API to get the config/hook?

@RonnyPfannschmidt
Copy link
Member

,items[0].config

@hroncok
Copy link
Member Author

hroncok commented Jan 26, 2021

Thanks @RonnyPfannschmidt

ftobia/pytest-ordering#76

@hroncok hroncok closed this as completed Jan 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants