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

Allow users to subclass FileLock with custom keyword arguments #284

Merged
merged 2 commits into from Oct 30, 2023

Conversation

hmaarrfk
Copy link
Contributor

@hmaarrfk hmaarrfk commented Oct 30, 2023

Without this patch:

=============================================== FAILURES ===============================================
______________________________ test_subclass_compatibility[UnixFileLock] _______________________________

lock_type = <class 'filelock._unix.UnixFileLock'>

    @pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
    def test_subclass_compatibility(lock_type):
        class MyFileLock(lock_type):
            def __init__(self, *args, my_param=None, **kwargs):
                pass
    
>       MyFileLock(my_param=1)
E       TypeError: BaseFileLock.__new__() got an unexpected keyword argument 'my_param'

tests/test_filelock.py:622: TypeError
______________________________ test_subclass_compatibility[SoftFileLock] _______________________________

lock_type = <class 'filelock._soft.SoftFileLock'>

    @pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
    def test_subclass_compatibility(lock_type):
        class MyFileLock(lock_type):
            def __init__(self, *args, my_param=None, **kwargs):
                pass
    
>       MyFileLock(my_param=1)
E       TypeError: BaseFileLock.__new__() got an unexpected keyword argument 'my_param'

tests/test_filelock.py:622: TypeError
======================================= short test summary info ========================================

Resolves the unstated issues in the comment: #282 (comment)

@hmaarrfk
Copy link
Contributor Author

With the updated test:

______________________________ test_subclass_compatibility[UnixFileLock] _______________________________

lock_type = <class 'filelock._unix.UnixFileLock'>
tmp_path = PosixPath('/tmp/pytest-of-mark/pytest-1/test_subclass_compatibility_Un0')

    @pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
    def test_subclass_compatibility(lock_type: type[BaseFileLock], tmp_path: Path) -> None:
        class MyFileLock(lock_type):
            def __init__(
                self,
                *args,  # noqa: ANN002
                my_param: int=0,
                **kwargs,  # noqa: ANN003
            ) -> None:
                pass
    
        lock_path = tmp_path / "a"
>       MyFileLock(str(lock_path), my_param=1)
E       TypeError: BaseFileLock.__new__() got an unexpected keyword argument 'my_param'

tests/test_filelock.py:628: TypeError
______________________________ test_subclass_compatibility[SoftFileLock] _______________________________

lock_type = <class 'filelock._soft.SoftFileLock'>
tmp_path = PosixPath('/tmp/pytest-of-mark/pytest-1/test_subclass_compatibility_So0')

    @pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
    def test_subclass_compatibility(lock_type: type[BaseFileLock], tmp_path: Path) -> None:
        class MyFileLock(lock_type):
            def __init__(
                self,
                *args,  # noqa: ANN002
                my_param: int=0,
                **kwargs,  # noqa: ANN003
            ) -> None:
                pass
    
        lock_path = tmp_path / "a"
>       MyFileLock(str(lock_path), my_param=1)
E       TypeError: BaseFileLock.__new__() got an unexpected keyword argument 'my_param'

tests/test_filelock.py:628: TypeError
======================================= short test summary info ========================================
FAILED tests/test_filelock.py::test_subclass_compatibility[UnixFileLock] - TypeError: BaseFileLock.__new__() got an unexpected keyword argument 'my_param'
FAILED tests/test_filelock.py::test_subclass_compatibility[SoftFileLock] - TypeError: BaseFileLock.__new__() got an unexpected keyword argument 'my_param'
========================================== 2 failed in 0.12s ===========================================

@hmaarrfk
Copy link
Contributor Author

Thank you for your consideration

@hmaarrfk
Copy link
Contributor Author

hmm. the cis were running. now they are not.

strange. do they need to be approved one at a time for me???

Copy link
Member

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

What's your use case for subclassing?

@hmaarrfk
Copy link
Contributor Author

I guess I had an implementation of #192 a while back (this got merged in in 2022, but we have had our own class since 2020)
https://github.com/ramonaoptics/python-multiuserfilelock/

and at the time we decided to use subclasses. We have since learned that subclassing creates these kinds of potential issues and we have generally moved away from using new subclasses. However this code is still in use today.

@hmaarrfk
Copy link
Contributor Author

your help resolving the mypy issues would be greatly appreciated.

@gaborbernat gaborbernat marked this pull request as draft October 30, 2023 15:57
@hmaarrfk hmaarrfk marked this pull request as ready for review October 30, 2023 17:24
Copy link
Member

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

@gaborbernat gaborbernat merged commit 141f5d8 into tox-dev:main Oct 30, 2023
31 checks passed
@hmaarrfk
Copy link
Contributor Author

thank you for the fast review

@hmaarrfk
Copy link
Contributor Author

and for the fast release

@tornaria
Copy link

Note the added test gives a PytestUnraisableExceptionWarning:

=============================== warnings summary ===============================
tests/test_filelock.py::test_subclass_compatibility
  /usr/lib/python3.12/site-packages/_pytest/unraisableexception.py:78: PytestUnraisableExceptionWarning: Exception ignored in: <function BaseFileLock.__del__ at 0x7fc8f0cda0c0>
  
  Traceback (most recent call last):
    File "/builddir/python3-filelock-3.13.1/.xbps-testdir/1705527530/usr/lib/python3.12/site-packages/filelock/_api.py", line 317, in __del__
      self.release(force=True)
    File "/builddir/python3-filelock-3.13.1/.xbps-testdir/1705527530/usr/lib/python3.12/site-packages/filelock/_api.py", line 280, in release
      if self.is_locked:
         ^^^^^^^^^^^^^^
    File "/builddir/python3-filelock-3.13.1/.xbps-testdir/1705527530/usr/lib/python3.12/site-packages/filelock/_api.py", line 190, in is_locked
      return self._context.lock_file_fd is not None
             ^^^^^^^^^^^^^
  AttributeError: 'MySoftFileLock' object has no attribute '_context'
  
    warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================== 83 passed, 20 skipped, 1 warning in 14.57s ==================

You need to call the parent class __init__, for instance:

--- a/tests/test_filelock.py
+++ b/tests/test_filelock.py
@@ -624,7 +624,8 @@ def test_subclass_compatibility(tmp_path: Path) -> None:
             my_param: int = 0,
             **kwargs: dict[str, Any],
         ) -> None:
-            pass
+            FileLock.__init__(self, lock_file, timeout, mode,
+                              thread_local, **kwargs)
 
     lock_path = tmp_path / "a"
     MyFileLock(str(lock_path), my_param=1)
@@ -639,7 +640,8 @@ def test_subclass_compatibility(tmp_path: Path) -> None:
             my_param: int = 0,
             **kwargs: dict[str, Any],
         ) -> None:
-            pass
+            SoftFileLock.__init__(self, lock_file, timeout, mode,
+                                  thread_local, **kwargs)
 
     MySoftFileLock(str(lock_path), my_param=1)
 

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

Successfully merging this pull request may close these issues.

None yet

3 participants