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

Raise NotImplementedError if using wrong class #135

Merged
merged 11 commits into from Feb 17, 2022
7 changes: 7 additions & 0 deletions docs/changelog.rst
@@ -1,6 +1,13 @@
Changelog
=========

Future
vonschultz marked this conversation as resolved.
Show resolved Hide resolved
------
- Fix pylint warning "Abstract class :class:`WindowsFileLock <filelock.WindowsFileLock>` with abstract methods instantiated"
:pr:`135` - by :user:`vonschultz`
- Fix pylint warning "Abstract class :class:`UnixFileLock <filelock.UnixFileLock>` with abstract methods instantiated"
:pr:`135` - by :user:`vonschultz`

v3.4.2 (2021-12-16)
-------------------
- Drop support for python ``3.6``
Expand Down
9 changes: 7 additions & 2 deletions src/filelock/_unix.py
Expand Up @@ -2,7 +2,6 @@

import os
import sys
from abc import ABC
from typing import cast

from ._api import BaseFileLock
Expand All @@ -11,9 +10,15 @@
has_fcntl = False
if sys.platform == "win32": # pragma: win32 cover

class UnixFileLock(BaseFileLock, ABC):
class UnixFileLock(BaseFileLock):
"""Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems."""

def _acquire(self) -> None:
raise NotImplementedError

def _release(self) -> None:
raise NotImplementedError

else: # pragma: win32 no cover
try:
import fcntl
Expand Down
9 changes: 7 additions & 2 deletions src/filelock/_windows.py
Expand Up @@ -2,7 +2,6 @@

import os
import sys
from abc import ABC
from errno import ENOENT
from typing import cast

Expand Down Expand Up @@ -49,9 +48,15 @@ def _release(self) -> None:

else: # pragma: win32 no cover

class WindowsFileLock(BaseFileLock, ABC):
class WindowsFileLock(BaseFileLock):
"""Uses the :func:`msvcrt.locking` function to hard lock the lock file on windows systems."""

def _acquire(self) -> None:
raise NotImplementedError

def _release(self) -> None:
raise NotImplementedError


__all__ = [
"WindowsFileLock",
Expand Down
35 changes: 34 additions & 1 deletion tests/test_filelock.py
@@ -1,5 +1,6 @@
from __future__ import annotations

import inspect
import logging
import sys
import threading
Expand All @@ -13,7 +14,7 @@
import pytest
from _pytest.logging import LogCaptureFixture

from filelock import BaseFileLock, FileLock, SoftFileLock, Timeout
from filelock import BaseFileLock, FileLock, SoftFileLock, Timeout, UnixFileLock, WindowsFileLock


@pytest.mark.parametrize(
Expand Down Expand Up @@ -368,3 +369,35 @@ def test_poll_intervall_deprecated(lock_type: type[BaseFileLock], tmp_path: Path
break
else: # pragma: no cover
pytest.fail("No warnings of stacklevel=2 matching.")


@pytest.mark.skipif(sys.platform != "win32", reason="Tests behavior of UnixFileLock on Windows systems")
def test_unix_lock_on_windows(tmp_path: Path) -> None:
lock = UnixFileLock(str(tmp_path / "lockfile"))

assert not inspect.isabstract(
UnixFileLock
), "UnixFileLock must not be an abstract class, or pylint will complain in client code"
assert inspect.isabstract(BaseFileLock)

with pytest.raises(NotImplementedError):
lock.acquire()

with pytest.raises(NotImplementedError):
lock._release()


@pytest.mark.skipif(sys.platform == "win32", reason="Tests behavior of WindowsFileLock on non-Windows systems")
def test_windows_lock_on_unix(tmp_path: Path) -> None:
lock = WindowsFileLock(str(tmp_path / "lockfile"))

assert not inspect.isabstract(
WindowsFileLock
), "WindowsFileLock must not be an abstract class, or pylint will complain in client code"
assert inspect.isabstract(BaseFileLock)

with pytest.raises(NotImplementedError):
lock.acquire()

with pytest.raises(NotImplementedError):
lock._release()
vonschultz marked this conversation as resolved.
Show resolved Hide resolved