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
=========

v3.6.0 (2022-02-17)
-------------------
- 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.5.1 (2022-02-16)
-------------------
- Use ``time.monotonic`` instead of ``time.time`` for calculating timeouts.
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
17 changes: 16 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 @@ -382,3 +383,17 @@ def decorated_method() -> None:
assert not lock.is_locked
decorated_method()
assert not lock.is_locked


def test_wrong_platform(tmp_path: Path) -> None:
assert not inspect.isabstract(UnixFileLock)
assert not inspect.isabstract(WindowsFileLock)
assert inspect.isabstract(BaseFileLock)

lock_type = UnixFileLock if sys.platform == "win32" else WindowsFileLock
lock = lock_type(str(tmp_path / "lockfile"))

with pytest.raises(NotImplementedError):
lock.acquire()
with pytest.raises(NotImplementedError):
lock._release()
1 change: 1 addition & 0 deletions whitelist.txt
Expand Up @@ -15,6 +15,7 @@ fmt
fspath
intersphinx
intervall
isabstract
iwgrp
iwoth
iwusr
Expand Down