Skip to content

Commit

Permalink
Replace usages of typing.cast
Browse files Browse the repository at this point in the history
It is no safer than type annotations and `type: ignore` comments, and it
doesn’t stand out as much as those do.
  • Loading branch information
dscorbett committed Nov 7, 2024
1 parent d6426a2 commit 7db2f10
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ builtins-ignorelist = [
inline-quotes = "single"
multiline-quotes = "single"

[tool.ruff.lint.flake8-tidy-imports.banned-api]
"typing.cast".msg = "Use a type annotation or `# type: ignore` instead"

[tool.ruff.lint.flake8-type-checking]
quote-annotations = true
strict = true
Expand Down
5 changes: 2 additions & 3 deletions sources/charsets/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from typing import Final
from typing import TYPE_CHECKING
from typing import assert_never
from typing import cast
import unicodedata

import gfsubsets
Expand Down Expand Up @@ -356,7 +355,7 @@ def initialize_schemas(charset: Charset, light_line: float, stroke_gap: float) -
high_vertical_secant = Line(90, secant=1 / 3)
rtl_secant = Line(240, secant=0.5, secant_curvature_offset=55)
ltr_secant = Line(310, secant=0.5, secant_curvature_offset=55)
tangent = Complex([lambda c: Context(None if c.angle is None else (c.angle - 90) % 360 if 90 < c.angle < 315 else (c.angle + 90) % 360), (0.25, Line(270), True), lambda c: Context((cast(float, c.angle) + 180) % 360), (0.5, Line(90))])
tangent = Complex([lambda c: Context(None if c.angle is None else (c.angle - 90) % 360 if 90 < c.angle < 315 else (c.angle + 90) % 360), (0.25, Line(270), True), lambda c: Context((c.angle + 180) % 360), (0.5, Line(90))]) # type: ignore[operator]
e_hook = Curve(90, 270, clockwise=True, hook=True, may_reposition_cursive_endpoints=True)
i_hook = Curve(180, 0, clockwise=False, hook=True, may_reposition_cursive_endpoints=True)
tangent_hook = TangentHook([(1, Curve(180, 270, clockwise=False), True), Context.as_reversed, (1, Curve(90, 270, clockwise=True))])
Expand Down Expand Up @@ -393,7 +392,7 @@ def initialize_schemas(charset: Charset, light_line: float, stroke_gap: float) -
first_quarter_moon = Complex([(0, Dot(1.5498)), (4, Line(180), True), (12, Line(270), True), (100, Curve(180, 180 - 10, clockwise=True), True), (100, Curve(360 - 10, 180 + 10, clockwise=False)), (100 / (1 + 0.3), Curve(10, 180 - 10, clockwise=True, stretch=0.3, stretch_axis=StretchAxis.ABSOLUTE)), (4.08, Line(70)), (200, Curve(3.8, 11.3, clockwise=False)), (200, Curve(180 + 11.3, 180 + 3.8, clockwise=True), True), (2, Line(80)), (1.5, Line(180), True), (20, Curve(0, 33.4, clockwise=False, stretch=1.4, long=True, stretch_axis=StretchAxis.ANGLE_OUT), True), (20, Curve(180 + 33.4, 90 + 33.4, clockwise=True, stretch=1.4, long=True)), (20, Curve(90 + 33.4, 37.8, clockwise=True, stretch=1.1, long=True, stretch_axis=StretchAxis.ANGLE_OUT)), (36, Curve(37.8, 125, clockwise=False, stretch=0.1, stretch_axis=StretchAxis.ANGLE_OUT)), (36, Curve(125, 68, clockwise=True, stretch=0.1, stretch_axis=StretchAxis.ABSOLUTE))])
full_moon = Complex([(100, Circle(0, 0, clockwise=False)), (4, Line(90), True), (200, Curve(180, 180 - 11.3, clockwise=True), True), (200, Curve(360 - 11.3, 11.3, clockwise=False)), (200, Curve(180 + 11.3, 180, clockwise=True), True), (8, Line(90), True), (4, Line(180), True), (0, Dot(1.5498)), (2, Line(90), True), (60, Curve(0, 360 - 22.9, clockwise=True), True), (60, Curve(180 - 22.9, 180 + 22.9, clockwise=False)), (60, Curve(22.9, 0, clockwise=True), True), (2, Line(270), True), (2 * 4, Line(0), True), (0, Dot(1.5498)), (2, Line(90), True), (60, Curve(0, 360 - 22.9, clockwise=True), True), (60, Curve(180 - 22.9, 180 + 22.9, clockwise=False)), (20, Curve(180 + 22.9, 276.1, clockwise=False)), (20, Curve(276.1, 282.2, clockwise=False, stretch=0.5, long=True, stretch_axis=StretchAxis.ANGLE_OUT)), (60, Curve(282.2, 288.3, clockwise=False, stretch=1, long=True, stretch_axis=StretchAxis.ANGLE_OUT)), (1, Line(288.3)), (25, Curve(180 + 53.3, 180 - 53.3, clockwise=True))])
last_quarter_moon = Complex([op._replace(shape=op.shape.clone(angle_in=180 - op.shape.angle_in, angle_out=180 - op.shape.angle_out, clockwise=not op.shape.clockwise) if isinstance(op.shape, Circle | Curve) else op.shape.clone(angle=180 - op.shape.angle) if isinstance(op.shape, Line) else op.shape) for op in first_quarter_moon.instructions]) # type: ignore[union-attr]
new_moon = Complex([*[(cast(float, op[0]) * full_moon.instructions[0].size * RADIUS / LINE_FACTOR, *op[1:]) for i in range(8) for op in [(math.sqrt(1 - ((i - (8 + 1) / 2 + 1) / (8 / 2)) ** 2) * (1.25 if i in {0, 8 - 1} else 0.95 if abs((i - (8 + 1) / 2 + 1) / (8 / 2)) < 0.5 else 1), Line(90), True), (2 * math.sqrt(1 - ((i - (8 + 1) / 2 + 1) / (8 / 2)) ** 2) * (1.25 if i in {0, 8 - 1} else 0.95 if abs((i - (8 + 1) / 2 + 1) / (8 / 2)) < 0.5 else 1), Line(270)), (math.sqrt(1 - ((i - (8 + 1) / 2 + 1) / (8 / 2)) ** 2) * (1.25 if i in {0, 8 - 1} else 0.95 if abs((i - (8 + 1) / 2 + 1) / (8 / 2)) < 0.5 else 1), Line(90), True), (2 / (8 + 1), Line(0), True)]], (full_moon.instructions[0].size * RADIUS / LINE_FACTOR * math.sqrt(2), Line(225), True), *full_moon.instructions]) # type: ignore[list-item, union-attr]
new_moon = Complex([*[(op[0] * full_moon.instructions[0].size * RADIUS / LINE_FACTOR, *op[1:]) for i in range(8) for op in [(math.sqrt(1 - ((i - (8 + 1) / 2 + 1) / (8 / 2)) ** 2) * (1.25 if i in {0, 8 - 1} else 0.95 if abs((i - (8 + 1) / 2 + 1) / (8 / 2)) < 0.5 else 1), Line(90), True), (2 * math.sqrt(1 - ((i - (8 + 1) / 2 + 1) / (8 / 2)) ** 2) * (1.25 if i in {0, 8 - 1} else 0.95 if abs((i - (8 + 1) / 2 + 1) / (8 / 2)) < 0.5 else 1), Line(270)), (math.sqrt(1 - ((i - (8 + 1) / 2 + 1) / (8 / 2)) ** 2) * (1.25 if i in {0, 8 - 1} else 0.95 if abs((i - (8 + 1) / 2 + 1) / (8 / 2)) < 0.5 else 1), Line(90), True), (2 / (8 + 1), Line(0), True)]], (full_moon.instructions[0].size * RADIUS / LINE_FACTOR * math.sqrt(2), Line(225), True), *full_moon.instructions]) # type: ignore[list-item, operator, union-attr]
line = Line(0)

enclosing_circle = Schema(None, circle, 10, anchor=anchors.MIDDLE)
Expand Down
5 changes: 2 additions & 3 deletions sources/duployan.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import math
from typing import Final
from typing import TYPE_CHECKING
from typing import cast

import fontTools.agl
import fontTools.feaLib.ast
Expand Down Expand Up @@ -493,10 +492,10 @@ def _recreate_gdef(self) -> None:

@staticmethod
def _glyph_to_schema(glyph: fontforge.glyph) -> Schema:
schema = glyph.temporary
schema: Schema = glyph.temporary
glyph.temporary = None
schema.glyph = glyph
return cast(Schema, schema)
return schema

def convert_classes(
self,
Expand Down
3 changes: 1 addition & 2 deletions sources/phases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
import itertools
from typing import Final
from typing import TYPE_CHECKING
from typing import cast
from typing import overload
from typing import override

Expand Down Expand Up @@ -1267,7 +1266,7 @@ def run_phases(
break
case _:
might_have_feedback = True
features = {cast(str, lookup.feature) for lookup in lookups}
features: set[str] = {lookup.feature for lookup in lookups} # type: ignore[misc]
for output_schema in output_schemas:
all_output_schemas.add(output_schema)
new_input_schemas = OrderedSet()
Expand Down
6 changes: 3 additions & 3 deletions sources/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@

from __future__ import annotations

from collections.abc import Mapping
import enum
import functools
import re
from typing import Final
from typing import Self
from typing import TYPE_CHECKING
from typing import cast
from typing import override
import unicodedata

Expand Down Expand Up @@ -69,6 +67,7 @@
from collections.abc import Collection
from collections.abc import Hashable
from collections.abc import Iterable
from collections.abc import Mapping
from collections.abc import MutableMapping
from collections.abc import MutableSequence
from collections.abc import Sequence
Expand Down Expand Up @@ -766,7 +765,8 @@ def _agl_name(cp: int) -> str:
ValueError: If `cp` is not ASCII.
"""
if cp <= 0x7F:
return cast(Mapping[int, str], fontTools.agl.UV2AGL)[cp]
uv2agl: Mapping[int, str] = fontTools.agl.UV2AGL
return uv2agl[cp]
raise ValueError

@staticmethod
Expand Down
8 changes: 4 additions & 4 deletions sources/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from typing import NamedTuple
from typing import Self
from typing import TYPE_CHECKING
from typing import cast
from typing import override

import fontTools.misc.transform
Expand Down Expand Up @@ -3354,7 +3353,8 @@ def boundingBox(self) -> tuple[float, float, float, float]:
minimum x, minimum y, maximum x, and maximum y.
"""
layer = self._stroke(copy=True)
return cast(tuple[float, float, float, float], layer.boundingBox())
bounding_box: tuple[float, float, float, float] = layer.boundingBox()
return bounding_box

def draw(
self,
Expand Down Expand Up @@ -4475,7 +4475,7 @@ def enter_on_first_path(self) -> bool:
def contextualize(self, context_in: Context, context_out: Context) -> Shape:
context_in = context_in.clone(ignorable_for_topography=False)
context_out = context_out.clone(ignorable_for_topography=False)
original_instructions = cast(list[Component], self.instructions)
original_instructions: Sequence[Component] = self.instructions # type: ignore[assignment]
if context_in == NO_CONTEXT and context_out != NO_CONTEXT:
assert context_out.angle is not None
outer_circle_op = original_instructions[0]
Expand Down Expand Up @@ -4760,7 +4760,7 @@ def contextualize(self, context_in: Context, context_out: Context) -> Shape:
), *self.instructions[3][2:]),
], _initial=True)
else:
shape = cast(Self, super())
shape = super() # type: ignore[assignment]
return shape.contextualize(context_in, context_out)


Expand Down

0 comments on commit 7db2f10

Please sign in to comment.