Skip to content

Commit

Permalink
Fix remaining type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Sep 20, 2023
1 parent 86755ec commit 55d6c45
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/app/reports.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Various reports that can be triggered from the options menu."""
from collections import Counter, defaultdict
from collections import defaultdict
from pathlib import Path, PurePosixPath
from typing import Dict, Set
from typing import Dict, Set, Counter

import srctools.logger
import trio
Expand Down Expand Up @@ -173,7 +173,7 @@ async def report_editor_models() -> None:
packset = get_loaded_packages()
fsys = FileSystemChain()
mat_to_usage: Dict[str, Set[str]] = defaultdict(set)
usage_counts: Counter[PurePosixPath] = Counter()
usage_counts = Counter[PurePosixPath]()

LOGGER.info('Checking existing packages...')
mdl_map_editor = PurePosixPath('resources/models/props_map_editor')
Expand Down
2 changes: 1 addition & 1 deletion src/app/tkMarkdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class RenderState:

if not hasattr(base_renderer.BaseRenderer, '__class_getitem__'):
# Patch in generic support.
base_renderer.BaseRenderer.__class_getitem__ = lambda item: base_renderer.BaseRenderer # type: ignore
base_renderer.BaseRenderer.__class_getitem__ = lambda item: base_renderer.BaseRenderer


class TKRenderer(base_renderer.BaseRenderer[SingleMarkdown]):
Expand Down
18 changes: 3 additions & 15 deletions src/app/tk_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,25 +331,13 @@ def __call__(self, func: EventFunc[AnyWidT], /) -> EventFunc[AnyWidT]:

class _Binder(Protocol):
@overload
def __call__(self, wid: WidgetT, *, add: bool=False) -> Callable[[_EventDeco[WidgetT]], _EventDeco[WidgetT]]:
...
def __call__(self, wid: WidgetT, *, add: bool=False) -> _EventDeco[WidgetT]: ... # type: ignore[overload-overlap]
@overload
def __call__(self, wid: tk.Misc, *, add: bool=False) -> Callable[[_EventDeco[tk.Misc]], _EventDeco[tk.Misc]]:
...
def __call__(self, wid: tk.Misc, *, add: bool=False) -> _EventDeco[tk.Misc]: ...
@overload
def __call__(self, wid: WidgetT, func: EventFunc[WidgetT], *, add: bool=False) -> str:
...
def __call__(self, wid: WidgetT, func: EventFunc[WidgetT], *, add: bool=False) -> str: ...
@overload
def __call__(self, wid: tk.Misc, func: EventFunc[tk.Misc], *, add: bool=False) -> str: ...
def __call__(
self, wid: Union[WidgetT, tk.Misc],
func: Union[EventFunc[WidgetT], EventFunc[tk.Misc], None] = None,
*, add: bool = False,
) -> Union[
Callable[[_EventDeco[WidgetT]], _EventDeco[WidgetT]],
Callable[[_EventDeco[tk.Misc]], _EventDeco[tk.Misc]],
str,
]: ...


def _bind_event_handler(bind_func: Callable[[WidgetT, EventFunc[WidgetT], bool], None]) -> _Binder:
Expand Down
19 changes: 12 additions & 7 deletions src/precomp/conditions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
from collections import defaultdict
from decimal import Decimal
from enum import Enum
from typing import Generic, Protocol, TypeVar, Any, Callable, TextIO, Tuple, Type, overload, cast
from typing import (
Generic, Protocol, TypeVar, Any, Callable, TextIO, Tuple, Type, Union, overload,
cast,
)

import attrs
import srctools.logger
Expand Down Expand Up @@ -79,10 +82,12 @@
ALL_META: list[tuple[str, Decimal, CondCall[object]]] = []


ResultT = TypeVar('ResultT')
CallableT = TypeVar('CallableT', bound=Callable[..., object])
# The return values for 2-stage results and flags.
FlagCallable = Callable[[Entity], bool]
ResultCallable = Callable[[Entity], object]
FlagCallT = TypeVar('FlagCallT', bound=Callable[..., Union[bool, FlagCallable]])


class SWITCH_TYPE(Enum):
Expand Down Expand Up @@ -599,9 +604,9 @@ def x(func: CallableT) -> CallableT:
return x


def make_flag(orig_name: str, *aliases: str) -> Callable[[CallableT], CallableT]:
def make_flag(orig_name: str, *aliases: str) -> Callable[[FlagCallT], FlagCallT]:
"""Decorator to add flags to the lookup."""
def x(func: CallableT) -> CallableT:
def x(func: FlagCallT) -> FlagCallT:
wrapper: CondCall[bool] = CondCall(func, _get_cond_group(func))
ALL_FLAGS.append((orig_name, aliases, wrapper))
name = orig_name.casefold()
Expand All @@ -625,7 +630,7 @@ def make_result(orig_name: str, *aliases: str) -> Callable[[CallableT], Callable
if name.casefold() != folded_name
])

def x(result_func: CallableT) -> CallableT:
def x(result_func: Callable[..., ResultT]) -> Callable[..., ResultT]:
"""Create the result when the function is supplied."""
# Legacy setup func support.
func: Callable[..., Callable[[Entity], object] | object]
Expand All @@ -652,7 +657,7 @@ def x(result_func: CallableT) -> CallableT:
assert alias_setup is setup_func, alias_setup
ALL_RESULTS.append((orig_name, aliases, wrapper))
return result_func
return x
return x # type: ignore[return-value] # Callable[..., T] -> TypeVar(bound=Callable)


def make_result_setup(*names: str) -> Callable[[CallableT], CallableT]:
Expand Down Expand Up @@ -1057,7 +1062,7 @@ def func(target: str | Entity | Solid, /, **kwargs: ValidKVs) -> Entity | Solid:
return Entity(vmf, keys={'classname': target})
return target

return func
return func # type: ignore[return-value]

for visgroup in vmf.vis_tree:
if visgroup.name == vis_name:
Expand Down Expand Up @@ -1088,7 +1093,7 @@ def adder(target: str | Entity | Solid, /, **kwargs: ValidKVs) -> Entity | Solid
target.hidden = True
return target

return adder
return adder # type: ignore[return-value]


def widen_fizz_brush(brush: Solid, thickness: float, bounds: tuple[Vec, Vec] | None = None) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/precomp/conditions/catwalks.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def res_make_catwalk(vmf: VMF, res: Keyvalues) -> object:
# Set the marker instances based on the attached walkways.
normal = FrozenVec(0, 0, 1) @ Angle.from_str(inst['angles'])
origin = Vec.from_str(inst['origin'])
pos_tup = origin.as_tuple()
pos_tup: Tuple[float, float, float] = origin.as_tuple()
dir_mask = catwalks[pos_tup]
angle = conditions.INST_ANGLE[normal]

Expand Down
11 changes: 10 additions & 1 deletion src/precomp/conditions/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,16 @@ def flag_offset_distance(inst: Entity, flag: Keyvalues) -> bool:
except ValueError:
return False

return INSTVAR_COMP.get(op, operator.eq)(offset, value)
func = INSTVAR_COMP.get(op, operator.eq)

try:
return bool(func(offset, value))
except (TypeError, ValueError) as exc:
LOGGER.warning(
'Distance comparison failed: {} {} {}',
offset, op, value, exc_info=exc,
)
return False


@make_result('rename', 'changeInstance')
Expand Down

0 comments on commit 55d6c45

Please sign in to comment.