Skip to content

Commit

Permalink
Use Ruff TYPE_CHECKING blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerYep committed Mar 29, 2024
1 parent d53e8c1 commit 2638a65
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
8 changes: 5 additions & 3 deletions probs/discrete/rv.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import annotations

import operator
from collections.abc import Callable
from dataclasses import dataclass, field
from typing import Any, TypeVar, cast
from typing import TYPE_CHECKING, Any, TypeVar, cast

from probs.floats import ApproxFloat
from probs.rv import Event, RandomVariable

if TYPE_CHECKING:
from collections.abc import Callable

T = TypeVar("T")


Expand Down Expand Up @@ -108,7 +110,7 @@ def pdf(self, x: float) -> float:
because this way allows us to access the pmf's keys internally without
accidentally adding empty values.
"""
return self.pmf[x] if x in self.pmf else 0
return self.pmf.get(x, 0)

def cdf(self, x: float) -> float:
"""
Expand Down
5 changes: 4 additions & 1 deletion probs/operations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

from probs.rv import Event, RandomVariable
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from probs.rv import Event, RandomVariable

SYNTAX_WARNING = (
"The {} syntax is not supported because it adds unnecessary "
Expand Down
28 changes: 13 additions & 15 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
target-version = "py312"
select = ["ALL"]
ignore = [
"ANN101", # Missing type annotation for `self` in method
"ANN102", # Missing type annotation for `cls` in classmethod
lint.select = ["ALL"]
lint.ignore = [
"ANN101", # Missing type annotation for `self` in method
"ANN102", # Missing type annotation for `cls` in classmethod
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed
"C901", # function is too complex (12 > 10)
"COM812", # Trailing comma missing
Expand All @@ -15,28 +15,26 @@ ignore = [
"FBT003", # Boolean positional value in function call
"FIX002", # Line contains TODO
"ISC001", # Isort
"PLR0911", # Too many return statements (11 > 6)
"PLR0911", # Too many return statements (11 > 6)
"PLR2004", # Magic value used in comparison, consider replacing 2 with a constant variable
"PLR0912", # Too many branches
"PLR0912", # Too many branches
"PLR0913", # Too many arguments to function call
"PLR0915", # Too many statements
"S101", # Use of `assert` detected
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
"T201", # print() found
"T203", # pprint() found
"TCH001", # Move application import into a type-checking block
"TCH003", # Move standard library import into a type-checking block
"TD002", # Missing author in TODO; try: `# TODO(<author_name>): ...`
"TD003", # Missing issue link on the line following this TODO
"TD005", # Missing issue description after `TODO`
"TRY003", # Avoid specifying long messages outside the exception class
"TD002", # Missing author in TODO; try: `# TODO(<author_name>): ...`
"TD003", # Missing issue link on the line following this TODO
"TD005", # Missing issue description after `TODO`
"TRY003", # Avoid specifying long messages outside the exception class

# Edutorch-specific ignores
"RUF001", # String contains ambiguous `α`
"RUF002", # Docstring contains ambiguous `α`
"RUF001", # String contains ambiguous `α`
"RUF002", # Docstring contains ambiguous `α`
"N802", # Function name should be lowercase
"NPY002", # Replace legacy `np.random.randn` call with `np.random.Generator`
]

[flake8-pytest-style]
[lint.flake8-pytest-style]
fixture-parentheses = false

0 comments on commit 2638a65

Please sign in to comment.