Skip to content

Commit d42a6da

Browse files
committed
fix(types): typing fixes exposed by extra checking
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 9f43ccc commit d42a6da

File tree

9 files changed

+23
-19
lines changed

9 files changed

+23
-19
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ build-backend = "poetry.core.masonry.api"
5454

5555
[tool.mypy]
5656
files = ["rich"]
57-
warn_unused_configs = true
5857
show_error_codes = true
5958
strict = true
60-
enable_error_code = ["ignore-without-code"]
59+
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
6160

6261
[[tool.mypy.overrides]]
6362
module = ["pygments.*", "IPython.*", "commonmark.*", "ipywidgets.*"]

rich/console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,7 @@ def _caller_frame_info(
19471947
frame = currentframe()
19481948
if frame is not None:
19491949
# Use the faster currentframe where implemented
1950-
while offset and frame:
1950+
while offset and frame is not None:
19511951
frame = frame.f_back
19521952
offset -= 1
19531953
assert frame is not None

rich/jupyter.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from typing import TYPE_CHECKING, Any, Dict, Iterable, List
1+
from typing import Any, Dict, Iterable, List, TYPE_CHECKING, Sequence
2+
3+
if TYPE_CHECKING:
4+
from rich.console import ConsoleRenderable
25

36
from . import get_console
47
from .segment import Segment
@@ -20,7 +23,7 @@ def __init__(self, html: str, text: str) -> None:
2023
self.text = text
2124

2225
def _repr_mimebundle_(
23-
self, include: Iterable[str], exclude: Iterable[str], **kwargs: Any
26+
self, include: Sequence[str], exclude: Sequence[str], **kwargs: Any
2427
) -> Dict[str, str]:
2528
data = {"text/plain": self.text, "text/html": self.html}
2629
if include:
@@ -37,8 +40,8 @@ class JupyterMixin:
3740

3841
def _repr_mimebundle_(
3942
self: "ConsoleRenderable",
40-
include: Iterable[str],
41-
exclude: Iterable[str],
43+
include: Sequence[str],
44+
exclude: Sequence[str],
4245
**kwargs: Any,
4346
) -> Dict[str, str]:
4447
console = get_console()

rich/measure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from operator import itemgetter
2-
from typing import TYPE_CHECKING, Callable, Iterable, NamedTuple, Optional
2+
from typing import TYPE_CHECKING, Callable, NamedTuple, Optional, Sequence
33

44
from . import errors
55
from .protocol import is_renderable, rich_cast
@@ -125,7 +125,7 @@ def get(
125125
def measure_renderables(
126126
console: "Console",
127127
options: "ConsoleOptions",
128-
renderables: Iterable["RenderableType"],
128+
renderables: Sequence["RenderableType"],
129129
) -> "Measurement":
130130
"""Get a measurement that would fit a number of renderables.
131131

rich/pretty.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Iterable,
2020
List,
2121
Optional,
22+
Sequence,
2223
Set,
2324
Tuple,
2425
Union,
@@ -28,8 +29,10 @@
2829

2930
try:
3031
import attr as _attr_module
32+
33+
_has_attrs = True
3134
except ImportError: # pragma: no cover
32-
_attr_module = None # type: ignore[assignment]
35+
_has_attrs = False
3336

3437
from . import get_console
3538
from ._loop import loop_last
@@ -54,12 +57,12 @@
5457

5558
def _is_attr_object(obj: Any) -> bool:
5659
"""Check if an object was created with attrs module."""
57-
return _attr_module is not None and _attr_module.has(type(obj))
60+
return _has_attrs and _attr_module.has(type(obj))
5861

5962

60-
def _get_attr_fields(obj: Any) -> Iterable["_attr_module.Attribute[Any]"]:
63+
def _get_attr_fields(obj: Any) -> Sequence["_attr_module.Attribute[Any]"]:
6164
"""Get fields for an attrs object."""
62-
return _attr_module.fields(type(obj)) if _attr_module is not None else []
65+
return _attr_module.fields(type(obj)) if _has_attrs else []
6366

6467

6568
def _is_dataclass_repr(obj: object) -> bool:

rich/progress.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,7 @@ def __init__(
643643
disable: bool = False,
644644
expand: bool = False,
645645
) -> None:
646-
assert (
647-
refresh_per_second is None or refresh_per_second > 0
648-
), "refresh_per_second must be > 0"
646+
assert refresh_per_second > 0, "refresh_per_second must be > 0"
649647
self._lock = RLock()
650648
self.columns = columns or self.get_default_columns()
651649
self.speed_estimate_period = speed_estimate_period

rich/spinner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def render(self, time: float) -> "RenderableType":
7979
self.speed = self._update_speed
8080
self._update_speed = 0.0
8181

82-
if not self.text:
82+
# This normally can't be str, unless someone assigned it later.
83+
if not self.text: # type: ignore[truthy-bool]
8384
return frame
8485
elif isinstance(self.text, (str, Text)):
8586
return Text.assemble(frame, " ", self.text)

rich/style.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ def __add__(self, style: Optional["Style"]) -> "Style":
740740
new_style._link = style._link or self._link
741741
new_style._link_id = style._link_id or self._link_id
742742
new_style._hash = style._hash
743-
new_style._null = self._null or style._null
743+
new_style._null = style._null
744744
if self._meta and style._meta:
745745
new_style._meta = dumps({**self.meta, **style.meta})
746746
else:

rich/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def add(
4545
style: Optional[StyleType] = None,
4646
guide_style: Optional[StyleType] = None,
4747
expanded: bool = True,
48-
highlight: bool = False,
48+
highlight: Optional[bool] = False,
4949
) -> "Tree":
5050
"""Add a child tree.
5151

0 commit comments

Comments
 (0)