Skip to content

Commit

Permalink
Merge pull request #233 from randovania/feature/lazy-imports
Browse files Browse the repository at this point in the history
Update ruff rules, use if TYPE_CHECKING/future annotations
  • Loading branch information
henriquegemignani authored Oct 15, 2024
2 parents 430c6ef + d79c46d commit d41e7e7
Show file tree
Hide file tree
Showing 139 changed files with 606 additions and 146 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependency.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

- name: Automatically approve the PR
uses: hmarr/auto-approve-action@v4
uses: hmarr/auto-approve-action@v4
29 changes: 26 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,34 @@ filterwarnings = [

[tool.ruff]
line-length = 120
lint.select = ["E", "F", "W", "C90", "I", "UP"]
src = ["src"]

# Version to target for generated code.
target-version = "py310"
[tool.ruff.lint]
select = [
"E", "F", "W", "C90", "I", "UP", "C4",
"RSE",
"TCH",
# "PTH",
"COM818", "COM819",
"ISC",
"PIE",
# "PT",

"PLC",
"PLC0208", # iterating over set is also not deterministic, so we shouldn't do that!

"PLE",
"PLR0402", "PLR1711", "PLR1722", "PLR0206", "PLR0133", "PLR0124",
"PLW",
"SIM101",
]
extend-ignore = [
"ISC001", # may cause conflicts with formatter
"PLW2901", # `for` loop variable `key` overwritten by assignment target
]

[tool.ruff.lint.isort]
required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.mccabe]
# Flag errors (`C901`) whenever the complexity level exceeds 25.
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
import subprocess
import sys
Expand Down
2 changes: 2 additions & 0 deletions src/mercury_engine_data_structures/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from mercury_engine_data_structures import cli

if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions src/mercury_engine_data_structures/__pyinstaller/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os

# Functions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from PyInstaller.utils.hooks import collect_data_files

# https://pyinstaller.readthedocs.io/en/stable/hooks.html#provide-hooks-with-package
Expand Down
2 changes: 2 additions & 0 deletions src/mercury_engine_data_structures/_dread_data_construct.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import struct

import construct
Expand Down
2 changes: 2 additions & 0 deletions src/mercury_engine_data_structures/adapters/enum_adapter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from construct import Adapter, Enum, Int32ub


Expand Down
6 changes: 4 additions & 2 deletions src/mercury_engine_data_structures/adapters/offset.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import annotations

from construct import AdaptationError, Adapter


class OffsetAdapter(Adapter):
# stores offsets as indices

def _get_table(self, context):
raise NotImplementedError()
raise NotImplementedError

def _get_table_length(self, context):
raise NotImplementedError()
raise NotImplementedError

def _get_base_offset(self, context):
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import typing

from construct import Construct, Container

from mercury_engine_data_structures.game_check import Game

if typing.TYPE_CHECKING:
import typing_extensions
from construct import Construct, Container

from mercury_engine_data_structures.file_tree_editor import FileTreeEditor
from mercury_engine_data_structures.game_check import Game


class BaseResource:
Expand All @@ -24,7 +22,7 @@ def __init__(self, raw: Container, target_game: Game, editor: FileTreeEditor | N

@classmethod
def construct_class(cls, target_game: Game) -> Construct:
raise NotImplementedError()
raise NotImplementedError

@classmethod
def parse(cls, data: bytes, target_game: Game, editor: FileTreeEditor | None = None) -> typing_extensions.Self:
Expand Down
2 changes: 2 additions & 0 deletions src/mercury_engine_data_structures/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import argparse
import asyncio
import itertools
Expand Down
2 changes: 2 additions & 0 deletions src/mercury_engine_data_structures/common_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import copy
import functools
import struct
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import operator

import construct.expr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import io

import construct
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import enum

import construct
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re

import construct
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any

from construct import EnumIntegerString
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import construct
from construct import Const, Construct, FocusedSeq, Optional, stream_tell

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import construct
from construct.core import (
FixedSized,
Expand Down Expand Up @@ -50,7 +52,7 @@ def PaddedStringRobust(length, encoding):
macro = StringEncodedRobust(FixedSized(length, NullStripped(GreedyBytes, pad=encodingunit(encoding))), encoding)

def _emitfulltype(ksy, bitwise):
return dict(size=length, type="strz", encoding=encoding)
return {"size": length, "type": "strz", "encoding": encoding}

macro._emitfulltype = _emitfulltype
return macro
Expand Down Expand Up @@ -86,8 +88,8 @@ def _emitparse(code):

def _emitseq(ksy, bitwise):
return [
dict(id="lengthfield", type=lengthfield._compileprimitivetype(ksy, bitwise)),
dict(id="data", size="lengthfield", type="str", encoding=encoding),
{"id": "lengthfield", "type": lengthfield._compileprimitivetype(ksy, bitwise)},
{"id": "data", "size": "lengthfield", "type": "str", "encoding": encoding},
]

macro._emitseq = _emitseq
Expand Down Expand Up @@ -176,7 +178,7 @@ def read_util_term_{i}(io, this):
macro._emitparse = _emitparse

def _emitfulltype(ksy, bitwise):
return dict(type="strz", encoding=encoding)
return {"type": "strz", "encoding": encoding}

macro._emitfulltype = _emitfulltype

Expand Down Expand Up @@ -210,7 +212,7 @@ def GreedyStringRobust(encoding):
macro = StringEncodedRobust(GreedyBytes, encoding)

def _emitfulltype(ksy, bitwise):
return dict(size_eos=True, type="str", encoding=encoding)
return {"size_eos": True, "type": "str", "encoding": encoding}

macro._emitfulltype = _emitfulltype
return macro
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import enum
from functools import partial

Expand Down
2 changes: 2 additions & 0 deletions src/mercury_engine_data_structures/crc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Module for calculating CRC hashes with the algorithm and data used by Mercury Engine.
"""

from __future__ import annotations

_crc32_constants = [
0x00000000,
0x77073096,
Expand Down
2 changes: 2 additions & 0 deletions src/mercury_engine_data_structures/dread_data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import functools
import json
import typing
Expand Down
17 changes: 11 additions & 6 deletions src/mercury_engine_data_structures/file_tree_editor.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
from __future__ import annotations

import copy
import enum
import json
import logging
import os.path
import typing
from collections.abc import Iterator
from pathlib import Path

import construct

from mercury_engine_data_structures import dread_data, formats, samus_returns_data
from mercury_engine_data_structures.base_resource import AssetId, BaseResource, NameOrAssetId, resolve_asset_id
from mercury_engine_data_structures.formats import Toc
from mercury_engine_data_structures.formats.base_resource import AssetId, BaseResource, NameOrAssetId, resolve_asset_id
from mercury_engine_data_structures.formats.pkg import Pkg
from mercury_engine_data_structures.game_check import Game
from mercury_engine_data_structures.romfs import RomFs

if typing.TYPE_CHECKING:
from collections.abc import Iterator
from pathlib import Path

import construct

from mercury_engine_data_structures.romfs import RomFs

_T = typing.TypeVar("_T", bound=BaseResource)
logger = logging.getLogger(__name__)
Expand Down
8 changes: 7 additions & 1 deletion src/mercury_engine_data_structures/formats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from mercury_engine_data_structures.formats.bapd import Bapd
from mercury_engine_data_structures.formats.base_resource import AssetType, BaseResource
from mercury_engine_data_structures.formats.bcmdl import Bcmdl
from mercury_engine_data_structures.formats.bcskla import Bcskla
from mercury_engine_data_structures.formats.bctex import Bctex
Expand Down Expand Up @@ -47,6 +50,9 @@
from mercury_engine_data_structures.formats.toc import Toc
from mercury_engine_data_structures.formats.txt import Txt

if TYPE_CHECKING:
from mercury_engine_data_structures.base_resource import AssetType, BaseResource

ALL_FORMATS = {
"PKG": Pkg,
"BAPD": Bapd,
Expand Down
12 changes: 9 additions & 3 deletions src/mercury_engine_data_structures/formats/bapd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import construct
from __future__ import annotations

from mercury_engine_data_structures.formats.base_resource import BaseResource
from typing import TYPE_CHECKING

from mercury_engine_data_structures.base_resource import BaseResource
from mercury_engine_data_structures.formats.standard_format import game_model
from mercury_engine_data_structures.game_check import Game

if TYPE_CHECKING:
import construct

from mercury_engine_data_structures.game_check import Game

BAPD = game_model("sound::CAudioPresets", "2.3.2")

Expand Down
10 changes: 8 additions & 2 deletions src/mercury_engine_data_structures/formats/bcmdl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import construct
from construct.core import (
Array,
Expand All @@ -24,9 +28,11 @@
stream_seek,
)

from mercury_engine_data_structures.base_resource import BaseResource
from mercury_engine_data_structures.common_types import Float, StrId
from mercury_engine_data_structures.formats.base_resource import BaseResource
from mercury_engine_data_structures.game_check import Game

if TYPE_CHECKING:
from mercury_engine_data_structures.game_check import Game

# Partial implementation to generate material variants
# Based off Joschuka's MMDL implementation in Noesis
Expand Down
4 changes: 3 additions & 1 deletion src/mercury_engine_data_structures/formats/bcskla.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import construct
from construct.core import (
Adapter,
Expand All @@ -20,9 +22,9 @@
this,
)

from mercury_engine_data_structures.base_resource import BaseResource
from mercury_engine_data_structures.common_types import Float, VersionAdapter
from mercury_engine_data_structures.construct_extensions.alignment import AlignTo
from mercury_engine_data_structures.formats.base_resource import BaseResource
from mercury_engine_data_structures.formats.property_enum import PropertyEnumDoubleUnsafe
from mercury_engine_data_structures.game_check import Game

Expand Down
4 changes: 3 additions & 1 deletion src/mercury_engine_data_structures/formats/bctex.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from enum import Enum

import construct
from construct.core import Error

from mercury_engine_data_structures.base_resource import BaseResource
from mercury_engine_data_structures.common_types import StrId, UInt
from mercury_engine_data_structures.formats.base_resource import BaseResource
from mercury_engine_data_structures.game_check import Game

BlockType = construct.Enum(
Expand Down
12 changes: 9 additions & 3 deletions src/mercury_engine_data_structures/formats/bgsnds.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from construct import Construct
from __future__ import annotations

from typing import TYPE_CHECKING

from mercury_engine_data_structures.base_resource import BaseResource
from mercury_engine_data_structures.formats import standard_format
from mercury_engine_data_structures.formats.base_resource import BaseResource
from mercury_engine_data_structures.game_check import Game

if TYPE_CHECKING:
from construct import Construct

from mercury_engine_data_structures.game_check import Game


class Bgsnds(BaseResource):
Expand Down
12 changes: 9 additions & 3 deletions src/mercury_engine_data_structures/formats/bldef.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from construct import Construct, Container
from __future__ import annotations

from typing import TYPE_CHECKING

from mercury_engine_data_structures.base_resource import BaseResource
from mercury_engine_data_structures.formats import standard_format
from mercury_engine_data_structures.formats.base_resource import BaseResource
from mercury_engine_data_structures.game_check import Game

if TYPE_CHECKING:
from construct import Construct, Container

from mercury_engine_data_structures.game_check import Game


class Bldef(BaseResource):
Expand Down
Loading

0 comments on commit d41e7e7

Please sign in to comment.