Skip to content

Commit befd166

Browse files
vonschultzpre-commit-ci[bot]gaborbernat
authored
Raise NotImplementedError if using wrong class (#135)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Bernát Gábor <[email protected]>
1 parent 46e4d0b commit befd166

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

docs/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
v3.6.0 (2022-02-17)
5+
-------------------
6+
- Fix pylint warning "Abstract class :class:`WindowsFileLock <filelock.WindowsFileLock>` with abstract methods instantiated"
7+
:pr:`135` - by :user:`vonschultz`
8+
- Fix pylint warning "Abstract class :class:`UnixFileLock <filelock.UnixFileLock>` with abstract methods instantiated"
9+
:pr:`135` - by :user:`vonschultz`
10+
411
v3.5.1 (2022-02-16)
512
-------------------
613
- Use ``time.monotonic`` instead of ``time.time`` for calculating timeouts.

src/filelock/_unix.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44
import sys
5-
from abc import ABC
65
from typing import cast
76

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

14-
class UnixFileLock(BaseFileLock, ABC):
13+
class UnixFileLock(BaseFileLock):
1514
"""Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems."""
1615

16+
def _acquire(self) -> None:
17+
raise NotImplementedError
18+
19+
def _release(self) -> None:
20+
raise NotImplementedError
21+
1722
else: # pragma: win32 no cover
1823
try:
1924
import fcntl

src/filelock/_windows.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44
import sys
5-
from abc import ABC
65
from errno import ENOENT
76
from typing import cast
87

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

5049
else: # pragma: win32 no cover
5150

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

54+
def _acquire(self) -> None:
55+
raise NotImplementedError
56+
57+
def _release(self) -> None:
58+
raise NotImplementedError
59+
5560

5661
__all__ = [
5762
"WindowsFileLock",

tests/test_filelock.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import inspect
34
import logging
45
import sys
56
import threading
@@ -13,7 +14,7 @@
1314
import pytest
1415
from _pytest.logging import LogCaptureFixture
1516

16-
from filelock import BaseFileLock, FileLock, SoftFileLock, Timeout
17+
from filelock import BaseFileLock, FileLock, SoftFileLock, Timeout, UnixFileLock, WindowsFileLock
1718

1819

1920
@pytest.mark.parametrize(
@@ -382,3 +383,17 @@ def decorated_method() -> None:
382383
assert not lock.is_locked
383384
decorated_method()
384385
assert not lock.is_locked
386+
387+
388+
def test_wrong_platform(tmp_path: Path) -> None:
389+
assert not inspect.isabstract(UnixFileLock)
390+
assert not inspect.isabstract(WindowsFileLock)
391+
assert inspect.isabstract(BaseFileLock)
392+
393+
lock_type = UnixFileLock if sys.platform == "win32" else WindowsFileLock
394+
lock = lock_type(str(tmp_path / "lockfile"))
395+
396+
with pytest.raises(NotImplementedError):
397+
lock.acquire()
398+
with pytest.raises(NotImplementedError):
399+
lock._release()

whitelist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fmt
1515
fspath
1616
intersphinx
1717
intervall
18+
isabstract
1819
iwgrp
1920
iwoth
2021
iwusr

0 commit comments

Comments
 (0)