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

Add ruff #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,5 @@ jobs:
- name: Install dependencies
run: poetry install

- name: Lint
run: |
# stop the build if there are Python syntax errors or undefined names
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
poetry run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

- name: Run tests
run: poetry run pytest -vv --durations=0
19 changes: 19 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Lint
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Ruff Check
uses: chartboost/ruff-action@v1

- name: Ruff Format
uses: chartboost/ruff-action@v1
with:
args: "format --check"
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
args: [ --fix ]
- id: ruff-format
28 changes: 15 additions & 13 deletions label_inspector/analysis/analysis_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,38 @@ def my_field_1(self) -> v1:
from typing import Generic, Optional, List, Dict, TypeVar, Callable


T = TypeVar('T')
R = TypeVar('R')
T = TypeVar("T")
R = TypeVar("R")


def agg_all(items: List[T]) -> Optional[T]:
'''
"""
Returns the first item if all items are equal, otherwise None.
'''
"""
if len(items) == 0:
return None
it0 = items[0]
return it0 if all(items[i] == it0 for i in range(1, len(items))) else None


def agg_any(items: List[T]) -> List[T]:
'''
"""
Returns a list of unique items.
'''
"""
return list(set(items))


def agg_only(items: List[T]) -> Optional[T]:
'''
"""
Returns the first item if there is only one item, otherwise None.
'''
"""
return items[0] if len(items) == 1 else None


class field(Generic[R]):
def __init__(self, func: Callable[..., R]):
self.func = func
self._is_public_field = func.__name__[0] != '_'
self._is_public_field = func.__name__[0] != "_"

def __get__(self, obj, cls) -> R:
if obj is None:
Expand All @@ -106,10 +106,12 @@ def __get__(self, obj, cls) -> R:


def analysis_object(cls):
cls._FIELDS = [name
for name in dir(cls)
if name[0] != '_'
if hasattr(getattr(cls, name), '_is_public_field')]
cls._FIELDS = [
name
for name in dir(cls)
if name[0] != "_"
if hasattr(getattr(cls, name), "_is_public_field")
]
return cls


Expand Down
13 changes: 7 additions & 6 deletions label_inspector/analysis/char_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,24 @@ def script(self) -> str:

@field
def name(self) -> str:
return myunicode.name(self._char, f'Unknown character in {self.script} script')
return myunicode.name(self._char, f"Unknown character in {self.script} script")

@field
def codepoint(self) -> str:
return self.root.i.f.codepoint_hex(self._char)

@field
def link(self) -> str:
if self.type == 'emoji':
if self.type == "emoji":
return self.root.i.f.emoji_link(self._char)
else:
return self.root.i.f.char_link(self._char)

@field
def type(self) -> str:
# Refers to this char's parent grapheme to detect ZWJs in emoji sequences.
return 'emoji' \
if self._char == '\u200d' \
and myunicode.is_emoji(self.parent_grapheme.value) \
else self.root.i.f.type(self._char)
return (
"emoji"
if self._char == "\u200d" and myunicode.is_emoji(self.parent_grapheme.value)
else self.root.i.f.type(self._char)
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ def value(self) -> str:

@field
def chars(self) -> List[CharAnalysis]:
return [CharAnalysis(char, self)
for char in self.grapheme]
return [CharAnalysis(char, self) for char in self.grapheme]
81 changes: 43 additions & 38 deletions label_inspector/analysis/grapheme_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

@analysis_object
class GraphemeAnalysis(AnalysisBase):
'''
"""
Basic analysis of a grapheme (no confusables).
'''
"""

def __init__(self, grapheme: str, parent):
self.grapheme = grapheme
Expand Down Expand Up @@ -44,73 +44,78 @@ def chars(self) -> List[CharAnalysis]:
"""
Truncated char analysis.
"""
return self._chars_untruncated[:self.root.config.truncate_chars]
return self._chars_untruncated[: self.root.config.truncate_chars]

@field
def name(self) -> str:
"""
Name of the grapheme.
Emoji sequence, single-character name or Combined Character.
"""
return myunicode.emoji_zwj_sequence_name(self.grapheme) or \
myunicode.emoji_sequence_name(self.grapheme) or \
getattr(self._single_char, 'name', 'Combined Character')
return (
myunicode.emoji_zwj_sequence_name(self.grapheme)
or myunicode.emoji_sequence_name(self.grapheme)
or getattr(self._single_char, "name", "Combined Character")
)

@field
def codepoint(self) -> Optional[str]:
return getattr(self._single_char, 'codepoint', None)
return getattr(self._single_char, "codepoint", None)

@field
def link(self) -> Optional[str]:
if self.type == 'emoji':
if self.type == "emoji":
return self.root.i.f.emoji_link(self.grapheme)
else:
return getattr(self._single_char, 'link', None) or \
self.root.i.f.multi_char_link(self.grapheme)
return getattr(
self._single_char, "link", None
) or self.root.i.f.multi_char_link(self.grapheme)

@field
def script(self) -> str:
scr = myunicode.script_of(self.grapheme)
return scr if scr is not None else 'Combined'
return scr if scr is not None else "Combined"

@field
def type(self) -> str:
if self.grapheme == '\ufe0f': # because it is treated as emoji
return 'invisible'
if self.grapheme == "\ufe0f": # because it is treated as emoji
return "invisible"

if myunicode.is_emoji(self.grapheme):
return 'emoji'
return "emoji"

cls = agg_all([c.type for c in self._chars_untruncated])

return cls or 'special'
return cls or "special"

@field
def font_support_all_os(self) -> Optional[bool]:
if self.type == 'emoji':
if self.type == "emoji":
return self.root.i.f.font_support.check_support(self.grapheme)
else:
return aggregate_font_support([self.root.i.f.font_support.check_support(c) for c in self.grapheme])
return aggregate_font_support(
[self.root.i.f.font_support.check_support(c) for c in self.grapheme]
)

@field
def description(self) -> str:
if self.type == 'simple_letter':
return 'A-Z letter'
elif self.type == 'simple_number':
return '0-9 number'
elif self.type == 'other_letter':
return f'{self.script} letter'
elif self.type == 'other_number':
return f'{self.script} number'
elif self.type == 'hyphen':
return 'Hyphen'
elif self.type == 'dollarsign':
return 'Dollar sign'
elif self.type == 'underscore':
return 'Underscore'
elif self.type == 'emoji':
return 'Emoji'
elif self.type == 'invisible':
return 'Invisible character'
elif self.type == 'special':
return 'Special character'
if self.type == "simple_letter":
return "A-Z letter"
elif self.type == "simple_number":
return "0-9 number"
elif self.type == "other_letter":
return f"{self.script} letter"
elif self.type == "other_number":
return f"{self.script} number"
elif self.type == "hyphen":
return "Hyphen"
elif self.type == "dollarsign":
return "Dollar sign"
elif self.type == "underscore":
return "Underscore"
elif self.type == "emoji":
return "Emoji"
elif self.type == "invisible":
return "Invisible character"
elif self.type == "special":
return "Special character"
30 changes: 21 additions & 9 deletions label_inspector/analysis/grapheme_with_confusables_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def make_conf_analysis(confusable: str, parent) -> ConfusableAnalysis:

@analysis_object
class GraphemeWithConfusablesAnalysis(GraphemeAnalysis):
'''
"""
Grapheme analysis with added confusables.
'''
"""

def __init__(self, grapheme: str, parent):
super().__init__(grapheme, parent)
Expand All @@ -34,7 +34,9 @@ def _is_confusable(self) -> bool:
Is the grapheme confusable?
Uses the first character.
"""
return self.root.i.f.is_confusable(self.grapheme, simple=self.root.config.simple_confusables)
return self.root.i.f.is_confusable(
self.grapheme, simple=self.root.config.simple_confusables
)

@field
def _confusables_other_untruncated(self) -> List[ConfusableAnalysis]:
Expand All @@ -43,18 +45,26 @@ def _confusables_other_untruncated(self) -> List[ConfusableAnalysis]:
Uses the first character.
"""
# optimize for non-confusable characters
return [] if not self._is_confusable \
else [make_conf_analysis(conf_text, self)
for conf_text
in self.root.i.f.get_confusables(self.grapheme, simple=self.root.config.simple_confusables)]
return (
[]
if not self._is_confusable
else [
make_conf_analysis(conf_text, self)
for conf_text in self.root.i.f.get_confusables(
self.grapheme, simple=self.root.config.simple_confusables
)
]
)

@field
def confusables_other(self) -> List[ConfusableAnalysis]:
"""
Truncated confusables for the grapheme.
Uses the first character.
"""
return self._confusables_other_untruncated[:self.root.config.truncate_confusables]
return self._confusables_other_untruncated[
: self.root.config.truncate_confusables
]

@field
def confusables_canonical(self) -> Optional[ConfusableAnalysis]:
Expand All @@ -66,7 +76,9 @@ def confusables_canonical(self) -> Optional[ConfusableAnalysis]:
if not self._is_confusable:
return None

canonical = self.root.i.f.get_canonical(self.grapheme, simple=self.root.config.simple_confusables)
canonical = self.root.i.f.get_canonical(
self.grapheme, simple=self.root.config.simple_confusables
)
if canonical is None:
return None
return make_conf_analysis(canonical, self)
Loading