Skip to content

Commit

Permalink
[core] Run ruff, apply several fixes
Browse files Browse the repository at this point in the history
- remove execution rights from `events.py`, and `watchmedo.py`, files
- code simplifications here and there
- use a proxy function to guess the best observer class import
  • Loading branch information
BoboTiG committed Jul 26, 2024
1 parent ab5117a commit 0254c3d
Show file tree
Hide file tree
Showing 28 changed files with 372 additions and 518 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ task:
- python3.8 -m pip install -U pip
- python3.8 -m pip install -r requirements-tests.txt
lint_script:
- python3.8 -m flake8 docs src tests tools
- python3.8 -m ruff src
tests_script:
- python3.8 -bb -m pytest tests
21 changes: 5 additions & 16 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
branches:
- master
pull_request:
branches:
- '**'

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name != 'pull_request' && github.sha || '' }}
Expand All @@ -21,12 +19,12 @@ jobs:
fail-fast: false
matrix:
tox:
- name: Types
environment: types
timeout: 15
- name: Test
environment: py
timeout: 15
- name: mypy
environment: mypy
timeout: 15
os:
- name: Linux
matrix: linux
Expand All @@ -50,17 +48,8 @@ jobs:
- "pypy-3.9"
include:
- tox:
name: Flake8
environment: flake8
timeout: 5
python: "3.11"
os:
name: Linux
emoji: 🐧
runs-on: [ubuntu-latest]
- tox:
name: isort
environment: isort-ci
name: Linter
environment: lint
timeout: 5
python: "3.11"
os:
Expand Down
5 changes: 0 additions & 5 deletions .isort.cfg

This file was deleted.

1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Changelog

2024-xx-xx • `full history <https://github.com/gorakhargosh/watchdog/compare/v4.0.1...HEAD>`__

- [core] Run ``ruff``, apply several fixes (`#1033 <https://github.com/gorakhargosh/watchdog/pull/1033>`__)
- [fsevents] Add missing ``event_filter`` keyword-argument to ``FSEventsObserver.schedule()`` (`#1049 <https://github.com/gorakhargosh/watchdog/pull/1049>`__)
- Thanks to our beloved contributors: @BoboTiG

Expand Down
74 changes: 69 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,71 @@
[tool.black]
target-version = ["py38"]
[tool.mypy]
# Ensure we know what we do
warn_redundant_casts = true
warn_unused_ignores = true
warn_unused_configs = true

# Imports management
ignore_missing_imports = true
follow_imports = "skip"

# Ensure full coverage
disallow_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_calls = true

# Restrict dynamic typing (a little)
# e.g. `x: List[Any]` or x: List`
# disallow_any_generics = true

strict_equality = true

[tool.pytest]
addopts = """
--showlocals
-v
--cov=watchdog
--cov-report=term-missing
"""

[tool.ruff]
line-length = 120
safe = true
indent-width = 4
target-version = "py38"

[tool.ruff.lint]
extend-select = ["ALL"]
ignore = [
"ARG",
"ANN", # TODO: remove
"ANN002",
"ANN003",
"ANN401",
"B006",
"B023", # TODO: remove
"B028",
"BLE001",
"C90",
"COM",
"D",
"EM",
"ERA",
"FBT",
"FIX",
"ISC001",
"N", # Requires a major version number bump
"PERF203", # TODO: remove
"PL",
"PTH", # TODO: remove?
"S",
"TD",
"TRY003",
"UP", # TODO: when minimum python version will be 3.10
]
fixable = ["ALL"]

[tool.isort]
profile = "black"
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
docstring-code-format = true
3 changes: 1 addition & 2 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
eventlet
flake8
flaky
isort
pytest
pytest-cov
pytest-timeout
ruff
sphinx
mypy
types-PyYAML
15 changes: 0 additions & 15 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ source-dir = docs/source
build-dir = docs/build
all_files = 1

[flake8]
ignore =
# E203 whitespace before ':', but E203 is not PEP 8 compliant
E203
# W503 line break before binary operator, but W503 is not PEP 8 compliant
W503
max-line-length = 120

[upload_sphinx]
# Requires sphinx-pypi-upload to work.
upload-dir = docs/build/html

[tool:pytest]
addopts =
--showlocals
-v
--cov=watchdog
--cov-report=term-missing
42 changes: 14 additions & 28 deletions src/watchdog/events.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
:module: watchdog.events
""":module: watchdog.events
:synopsis: File system events and event handlers.
:author: yesudeep@google.com (Yesudeep Mangalapilly)
:author: contact@tiger-222.fr (Mickaël Schoentgen)
Expand Down Expand Up @@ -111,8 +110,7 @@

@dataclass(unsafe_hash=True)
class FileSystemEvent:
"""
Immutable type that represents a file system event that is triggered
"""Immutable type that represents a file system event that is triggered
when a change occurs on the monitored file system.
All FileSystemEvent objects are required to be immutable and hence
Expand Down Expand Up @@ -186,9 +184,7 @@ class DirDeletedEvent(FileSystemEvent):


class DirModifiedEvent(FileSystemEvent):
"""
File system event representing directory modification on the file system.
"""
"""File system event representing directory modification on the file system."""

event_type = EVENT_TYPE_MODIFIED
is_directory = True
Expand All @@ -208,9 +204,7 @@ class DirMovedEvent(FileSystemMovedEvent):


class FileSystemEventHandler:
"""
Base file system event handler that you can override methods from.
"""
"""Base file system event handler that you can override methods from."""

def dispatch(self, event: FileSystemEvent) -> None:
"""Dispatches events to the appropriate methods.
Expand Down Expand Up @@ -317,32 +311,28 @@ def __init__(

@property
def patterns(self):
"""
(Read-only)
"""(Read-only)
Patterns to allow matching event paths.
"""
return self._patterns

@property
def ignore_patterns(self):
"""
(Read-only)
"""(Read-only)
Patterns to ignore matching event paths.
"""
return self._ignore_patterns

@property
def ignore_directories(self):
"""
(Read-only)
"""(Read-only)
``True`` if directories should be ignored; ``False`` otherwise.
"""
return self._ignore_directories

@property
def case_sensitive(self):
"""
(Read-only)
"""(Read-only)
``True`` if path names should be matched sensitive to case; ``False``
otherwise.
"""
Expand Down Expand Up @@ -399,39 +389,35 @@ def __init__(
self._regexes = [re.compile(r) for r in regexes]
self._ignore_regexes = [re.compile(r) for r in ignore_regexes]
else:
self._regexes = [re.compile(r, re.I) for r in regexes]
self._ignore_regexes = [re.compile(r, re.I) for r in ignore_regexes]
self._regexes = [re.compile(r, re.IGNORECASE) for r in regexes]
self._ignore_regexes = [re.compile(r, re.IGNORECASE) for r in ignore_regexes]
self._ignore_directories = ignore_directories
self._case_sensitive = case_sensitive

@property
def regexes(self):
"""
(Read-only)
"""(Read-only)
Regexes to allow matching event paths.
"""
return self._regexes

@property
def ignore_regexes(self):
"""
(Read-only)
"""(Read-only)
Regexes to ignore matching event paths.
"""
return self._ignore_regexes

@property
def ignore_directories(self):
"""
(Read-only)
"""(Read-only)
``True`` if directories should be ignored; ``False`` otherwise.
"""
return self._ignore_directories

@property
def case_sensitive(self):
"""
(Read-only)
"""(Read-only)
``True`` if path names should be matched sensitive to case; ``False``
otherwise.
"""
Expand Down
63 changes: 34 additions & 29 deletions src/watchdog/observers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
:module: watchdog.observers
""":module: watchdog.observers
:synopsis: Observer that picks a native implementation if available.
:author: yesudeep@google.com (Yesudeep Mangalapilly)
:author: contact@tiger-222.fr (Mickaël Schoentgen)
Expand Down Expand Up @@ -52,47 +51,53 @@

from __future__ import annotations

import contextlib
import sys
import warnings
from typing import TYPE_CHECKING

from watchdog.utils import UnsupportedLibc

from .api import BaseObserverSubclassCallable

Observer: BaseObserverSubclassCallable
if TYPE_CHECKING:
from watchdog.observers.api import BaseObserverSubclassCallable


if sys.platform.startswith("linux"):
try:
from .inotify import InotifyObserver as Observer
except UnsupportedLibc:
from .polling import PollingObserver as Observer
def _get_observer_cls() -> BaseObserverSubclassCallable:
if sys.platform.startswith("linux"):
with contextlib.suppress(UnsupportedLibc):
from watchdog.observers.inotify import InotifyObserver

elif sys.platform.startswith("darwin"):
try:
from .fsevents import FSEventsObserver as Observer
except Exception:
return InotifyObserver
elif sys.platform.startswith("darwin"):
try:
from .kqueue import KqueueObserver as Observer

warnings.warn("Failed to import fsevents. Fall back to kqueue")
from watchdog.observers.fsevents import FSEventsObserver
except Exception:
try:
from watchdog.observers.kqueue import KqueueObserver
except Exception:
warnings.warn("Failed to import fsevents and kqueue. Fall back to polling.")
else:
warnings.warn("Failed to import fsevents. Fall back to kqueue")
return KqueueObserver
else:
return FSEventsObserver
elif sys.platform.startswith("win"):
try:
from watchdog.observers.read_directory_changes import WindowsApiObserver
except Exception:
from .polling import PollingObserver as Observer
warnings.warn("Failed to import `read_directory_changes`. Fall back to polling.")
else:
return WindowsApiObserver
elif sys.platform in {"dragonfly", "freebsd", "netbsd", "openbsd", "bsd"}:
from watchdog.observers.kqueue import KqueueObserver

warnings.warn("Failed to import fsevents and kqueue. Fall back to polling.")
return KqueueObserver

elif sys.platform in ("dragonfly", "freebsd", "netbsd", "openbsd", "bsd"):
from .kqueue import KqueueObserver as Observer
from watchdog.observers.polling import PollingObserver

elif sys.platform.startswith("win"):
try:
from .read_directory_changes import WindowsApiObserver as Observer
except Exception:
from .polling import PollingObserver as Observer
return PollingObserver

warnings.warn("Failed to import read_directory_changes. Fall back to polling.")

else:
from .polling import PollingObserver as Observer
Observer = _get_observer_cls()

__all__ = ["Observer"]
Loading

0 comments on commit 0254c3d

Please sign in to comment.