Skip to content

Commit

Permalink
Merge pull request #97 from pyvisa/ruff
Browse files Browse the repository at this point in the history
Use ruff for linting and formatting the codebase
  • Loading branch information
MatthieuDartiailh committed Mar 5, 2024
2 parents 2d51cc9 + c04e2c0 commit 6b79f5b
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 88 deletions.
14 changes: 0 additions & 14 deletions .coveragerc

This file was deleted.

14 changes: 0 additions & 14 deletions .flake8

This file was deleted.

14 changes: 5 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r dev-requirements.txt
- name: Isort
- name: Format
run: |
isort pyvisa_sim -c;
- name: Black
ruff format pyvisa_sim;
- name: Lint
if: always()
run: |
black pyvisa_sim --check;
- name: Flake8
if: always()
run: |
flake8 pyvisa_sim;
ruff check pyvisa_sim;
- name: Mypy
if: always()
run: |
Expand All @@ -58,7 +54,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand Down
20 changes: 8 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
repos:
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.3.0
hooks:
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991 # Use the sha / tag you want to point at
hooks:
Expand Down
4 changes: 1 addition & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
pyvisa
black
flake8
ruff
mypy
types-PyYAML
isort
pytest
sphinx
sphinx-rtd-theme
44 changes: 40 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = [
"pyvisa>=1.11.0",
Expand Down Expand Up @@ -80,8 +81,21 @@ del namedtuple, _version_info, parts
__version__ = "{version}"
"""

[tool.black]
line-length = 88 # Enforce the default value
[tool.ruff]
src = ["src"]
extend-exclude = ["pyvisa/thirdparty/*"]
line-length = 88

[tool.ruff.lint]
select = ["C", "E", "F", "W", "I", "C90", "RUF"]
extend-ignore = ["E501", "RUF012"]

[tool.ruff.lint.isort]
combine-as-imports = true
known-first-party = ["pyvisa"]

[tool.ruff.lint.mccabe]
max-complexity = 20

[tool.pytest.ini_options]
minversion = "6.0"
Expand All @@ -96,6 +110,28 @@ module = [
]
ignore_missing_imports = true

[tool.isort]
profile = "black"
[tool.coverage]
[tool.coverage.run]
branch = true
source = ["pyvisa_sim"]

[tool.coverage.report]
# Regexes for lines to exclude from consideration
exclude_lines = [
# Have to re-enable the standard pragma
"pragma: no cover",

# Don't complain if tests don't hit defensive assertion code:
"raise NotImplementedError",
"pass",

# Don't complain about abstract methods, they aren't run:
"@(abc\\.)?abstractmethod",

# Don't complain about type checking
"if TYPE_CHECKING:",

# Don't complain about ellipsis in overload
"\\.\\.\\.",
]

1 change: 1 addition & 0 deletions pyvisa_sim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

from importlib.metadata import PackageNotFoundError, version

from .highlevel import SimVisaLibrary
Expand Down
1 change: 1 addition & 0 deletions pyvisa_sim/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

from collections import defaultdict
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, TypeVar

Expand Down
9 changes: 6 additions & 3 deletions pyvisa_sim/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
:license: MIT, see LICENSE for more details.
"""

import logging
from typing import Callable, Iterator, Optional, Sequence
from typing import Iterator, Optional, Sequence

from pyvisa import logger

Expand Down Expand Up @@ -87,5 +88,7 @@ def iter_bytes(
raise ValueError(f"Unknown 'send_end' value '{send_end}'")


int_to_byte: Callable[[int], bytes] = lambda val: bytes([val])
last_int: Callable[[Sequence[int]], int] = lambda val: val[-1]
def int_to_byte(val: int) -> bytes:
return bytes([val])
def last_int(val: Sequence[int]) -> int:
return val[-1]
9 changes: 4 additions & 5 deletions pyvisa_sim/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

import enum
from typing import (
Dict,
Expand Down Expand Up @@ -40,13 +41,11 @@ class Responses(enum.Enum):


@overload
def to_bytes(val: str) -> bytes:
...
def to_bytes(val: str) -> bytes: ...


@overload
def to_bytes(val: Literal[Responses.NO]) -> Literal[Responses.NO]:
...
def to_bytes(val: Literal[Responses.NO]) -> Literal[Responses.NO]: ...


def to_bytes(val):
Expand Down Expand Up @@ -108,7 +107,7 @@ def __init__(self, specs: Dict[str, str]) -> None:

self.min = specs_type(specs["min"]) if "min" in specs else None
self.max = specs_type(specs["max"]) if "max" in specs else None
self.valid = set([specs_type(val) for val in specs.get("valid", ())])
self.valid = {specs_type(val) for val in specs.get("valid", ())}


class Property(Generic[T]):
Expand Down
1 change: 1 addition & 0 deletions pyvisa_sim/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

from typing import Dict, List, Optional, Tuple, Union

from pyvisa import constants, rname
Expand Down
7 changes: 3 additions & 4 deletions pyvisa_sim/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

import random
from collections import OrderedDict
from traceback import format_exc
Expand Down Expand Up @@ -71,12 +72,10 @@ def _init(self) -> None:
raise type(e)(msg % format_exc())

@overload
def _register(self, obj: "SimVisaLibrary") -> VISARMSession:
...
def _register(self, obj: "SimVisaLibrary") -> VISARMSession: ...

@overload
def _register(self, obj: Session) -> VISASession:
...
def _register(self, obj: Session) -> VISASession: ...

def _register(self, obj):
"""Creates a random but unique session handle for a session object.
Expand Down
3 changes: 2 additions & 1 deletion pyvisa_sim/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

import importlib.resources
import os
import pathlib
Expand Down Expand Up @@ -67,7 +68,7 @@ def _get_pair(dd: Dict[str, str]) -> Tuple[str, str]:


def _get_triplet(
dd: Dict[str, str]
dd: Dict[str, str],
) -> Tuple[str, Union[str, Literal[Responses.NO]], Union[str, Literal[Responses.NO]]]:
"""Return a triplet from a dialogue dictionary."""
return (
Expand Down
1 change: 1 addition & 0 deletions pyvisa_sim/sessions/gpib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

from pyvisa import constants, rname

from . import session
Expand Down
1 change: 1 addition & 0 deletions pyvisa_sim/sessions/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

import time
from typing import Tuple

Expand Down
7 changes: 4 additions & 3 deletions pyvisa_sim/sessions/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

import time
from typing import Any, Callable, Dict, Optional, Tuple, Type, TypeVar

Expand Down Expand Up @@ -34,9 +35,9 @@ class Session:

#: Maps (Interface Type, Resource Class) to Python class encapsulating that resource.
#: dict[(Interface Type, Resource Class) , Session]
_session_classes: Dict[
Tuple[constants.InterfaceType, str], Type["Session"]
] = dict()
_session_classes: Dict[Tuple[constants.InterfaceType, str], Type["Session"]] = (
{}
)

#: Session handler for the resource manager.
session_type: Tuple[constants.InterfaceType, str]
Expand Down
7 changes: 4 additions & 3 deletions pyvisa_sim/sessions/tcpip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

from pyvisa import constants, rname

from . import session
Expand All @@ -23,9 +24,9 @@ def after_parsing(self) -> None:
self.parsed.board
)
self.attrs[constants.ResourceAttribute.tcpip_address] = self.parsed.host_address
self.attrs[
constants.ResourceAttribute.tcpip_device_name
] = self.parsed.lan_device_name
self.attrs[constants.ResourceAttribute.tcpip_device_name] = (
self.parsed.lan_device_name
)


@session.Session.register(constants.InterfaceType.tcpip, "SOCKET")
Expand Down
13 changes: 7 additions & 6 deletions pyvisa_sim/sessions/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license: MIT, see LICENSE for more details.
"""

from typing import Union

from pyvisa import constants, rname
Expand All @@ -19,13 +20,13 @@ def after_parsing(self) -> None:
self.attrs[constants.ResourceAttribute.interface_number] = int(
self.parsed.board
)
self.attrs[
constants.ResourceAttribute.manufacturer_id
] = self.parsed.manufacturer_id
self.attrs[constants.ResourceAttribute.manufacturer_id] = (
self.parsed.manufacturer_id
)
self.attrs[constants.ResourceAttribute.model_code] = self.parsed.model_code
self.attrs[
constants.ResourceAttribute.usb_serial_number
] = self.parsed.serial_number
self.attrs[constants.ResourceAttribute.usb_serial_number] = (
self.parsed.serial_number
)
self.attrs[constants.ResourceAttribute.usb_interface_number] = int(
self.parsed.board
)
Expand Down
1 change: 1 addition & 0 deletions pyvisa_sim/testsuite/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import pytest

import pyvisa


Expand Down
1 change: 1 addition & 0 deletions pyvisa_sim/testsuite/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

import pytest

from pyvisa.errors import VisaIOError


Expand Down
1 change: 0 additions & 1 deletion pyvisa_sim/testsuite/test_serial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
import pyvisa

from pyvisa_sim.sessions import serial

serial.SerialInstrumentSession
Expand Down
6 changes: 0 additions & 6 deletions setup.py

This file was deleted.

0 comments on commit 6b79f5b

Please sign in to comment.