Skip to content

Use functools.lru_cache with _getconftest_pathlist #4227

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" command line options, ini-file and conftest.py processing. """
from __future__ import absolute_import, division, print_function
import argparse
import functools
import inspect
import shlex
import types
Expand Down Expand Up @@ -907,6 +908,10 @@ def _getconftest_pathlist(self, name, path):
values.append(relroot)
return values

if six.PY3:
# once we drop Python 2, please change this to use the normal decorator syntax (#4227)
_getconftest_pathlist = functools.lru_cache(maxsize=None)(_getconftest_pathlist)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Old school style! 😎

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\o/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw

if six.PY3:

is going to break when python4 comes out -- usually prefer if six.PY2: ... / if not six.PY2: ...

I've seen other projects do this in a different way:

if six.PY2:
    def lru_cache(*_, **__):
        def dec(fn):
            return fn
        return dec
else:
    from functools import lru_cache

neat trick though (saw this from twitter, missed the PR notification)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asottile
Please consider creating a follow up PR - otherwise I might do it for the follow up via #4224 (the removed, 2nd commit).
It should go into pytest.compat probably, so that it is easier to use everywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All fair points. I don't mind leaving the code as is, and also don't mind to change to the ones suggested here too. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will move it to compat now.


def _get_override_ini_value(self, name):
value = None
# override_ini is a list of "ini=value" options
Expand Down