Skip to content

test_sqlite3.test_userfunctions redefines test_func_return_too_large_int() method #105557

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

Closed
vstinner opened this issue Jun 9, 2023 · 5 comments · Fixed by #105558
Closed

test_sqlite3.test_userfunctions redefines test_func_return_too_large_int() method #105557

vstinner opened this issue Jun 9, 2023 · 5 comments · Fixed by #105558
Assignees
Labels
tests Tests in the Lib/test dir topic-sqlite3 type-bug An unexpected behavior, bug, or error

Comments

@vstinner
Copy link
Member

vstinner commented Jun 9, 2023

Lib/test/test_sqlite3/test_userfunctions.py defines to methods called test_func_return_too_large_int() in the same class, so the first one is never called.

cc @erlend-aasland

Linked PRs

@vstinner vstinner added the type-bug An unexpected behavior, bug, or error label Jun 9, 2023
@AlexWaygood AlexWaygood added tests Tests in the Lib/test dir topic-sqlite3 labels Jun 9, 2023
@erlend-aasland
Copy link
Contributor

Oh, that's unfortunate. Thanks for the report, Victor!

@erlend-aasland erlend-aasland self-assigned this Jun 9, 2023
@vstinner
Copy link
Member Author

vstinner commented Jun 9, 2023

What's unfortunate is that unittest has no metaclass using a prepare method with a custom dict type which detects such bug.

erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jun 9, 2023
test_func_return_too_large_int() was defined twice.
Keep only the redefined method, as that also checks the tracebacks.
@erlend-aasland
Copy link
Contributor

What's unfortunate is that unittest has no metaclass using a prepare method with a custom dict type which detects such bug.

+1

That would be a useful feature.

@vstinner
Copy link
Member Author

vstinner commented Jun 9, 2023

Prototype to detect such bug:

import inspect
import warnings

class MyDict(dict):
    def __init__(self, class_name):
        self.class_name = class_name

    def __setitem__(self, name, value):
        # TestLoader.testMethodPrefix = 'test'
        if name.startswith('test'):
            try:
                old_value = self[name]
            except KeyError:
                pass
            else:
                if inspect.isfunction(old_value):
                    warnings.warn(f"BUG: {self.class_name}.{name}() method redefined", stacklevel=2)
        dict.__setitem__(self, name, value)

class MyMetaClass(type):
    @classmethod
    def __prepare__(metacls, class_name, bases, **kwds):
        return MyDict(class_name)

class TestCase(metaclass=MyMetaClass):
    pass

class Tests(TestCase):
    def __init__(self):
        self.attr = 1
        self.attr = 2

    def test_bug(self):  # first defintion
        pass

    def test_bug(self):  # second definion
        pass


tests = Tests()

Output:

/home/vstinner/python/main/x.py:36: UserWarning: BUG: Tests.test_bug() method redefined
  def test_bug(self):  # second definion

@vstinner
Copy link
Member Author

vstinner commented Jun 9, 2023

That would be a useful feature.

I created issue #105560 to track this idea.

erlend-aasland added a commit that referenced this issue Jun 9, 2023
test_func_return_too_large_int() was defined twice.
Keep only the redefined method, as that also checks the tracebacks.
@erlend-aasland erlend-aasland linked a pull request Jun 9, 2023 that will close this issue
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 9, 2023
test_func_return_too_large_int() was defined twice.
Keep only the redefined method, as that also checks the tracebacks.
(cherry picked from commit b8fa7bd)

Co-authored-by: Erlend E. Aasland <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 9, 2023
test_func_return_too_large_int() was defined twice.
Keep only the redefined method, as that also checks the tracebacks.
(cherry picked from commit b8fa7bd)

Co-authored-by: Erlend E. Aasland <[email protected]>
erlend-aasland added a commit that referenced this issue Jun 9, 2023
…105562)

test_func_return_too_large_int() was defined twice.
Keep only the redefined method, as that also checks the tracebacks.
(cherry picked from commit b8fa7bd)

Co-authored-by: Erlend E. Aasland <[email protected]>
erlend-aasland added a commit that referenced this issue Jun 9, 2023
…105561)

test_func_return_too_large_int() was defined twice.
Keep only the redefined method, as that also checks the tracebacks.
(cherry picked from commit b8fa7bd)

Co-authored-by: Erlend E. Aasland <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir topic-sqlite3 type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants