Skip to content

Commit

Permalink
Fix a lot of typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jferard committed Oct 22, 2024
1 parent 8c8a653 commit 8786823
Show file tree
Hide file tree
Showing 25 changed files with 90 additions and 80 deletions.
10 changes: 7 additions & 3 deletions lib/py4lo_sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ class TransactionMode(enum.Enum):
sqlite3_lib.sqlite3_interrupt.restype = None
sqlite3_interrupt = sqlite3_lib.sqlite3_interrupt

sqlite3_lib.sqlite3_is_interrupted.argtypes = [sqlite3_p]
sqlite3_lib.sqlite3_is_interrupted.restype = c_int
sqlite3_is_interrupted = sqlite3_lib.sqlite3_is_interrupted
try:
sqlite3_lib.sqlite3_is_interrupted.argtypes = [sqlite3_p]
sqlite3_lib.sqlite3_is_interrupted.restype = c_int
sqlite3_is_interrupted = sqlite3_lib.sqlite3_is_interrupted
except AttributeError:
def sqlite3_is_interrupted(db):
return -1

# https://www.sqlite.org/c3ref/exec.html
sqlite3_lib.sqlite3_exec.argtypes = [sqlite3_p, c_char_p, c_void_p, c_void_p,
Expand Down
6 changes: 3 additions & 3 deletions py4lo/branch_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from typing import List, Callable, Any
from typing import List, Callable, Any, cast


class BranchProcessor:
Expand All @@ -29,13 +29,13 @@ class BranchProcessor:
def __init__(self, tester: Callable[[List[str]], bool]):
"""The tester will evaluate the arguments of 'if' or 'elif'"""
self._assertion_is_true = tester
self._dont_skips = []
self._dont_skips = cast(List[bool], [])

def end(self):
"""
To call before the end, to verify if there are no unclosed if block
"""
if len(self._dont_skips):
if self._dont_skips:
logging.error("Branch condition not closed!")
raise ValueError("Branch condition not closed!")

Expand Down
3 changes: 2 additions & 1 deletion py4lo/callbacks/add_debug_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pathlib import Path
from typing import Dict, List
from zipfile import ZipFile

Expand Down Expand Up @@ -149,7 +150,7 @@ class AddDebugContent(AfterCallback):
The debug content is one button per function.
"""

def __init__(self, funcs_by_script: Dict[str, List[str]]):
def __init__(self, funcs_by_script: Dict[Path, List[str]]):
"""
:param funcs_by_script: a mapping script_name -> [func name]
"""
Expand Down
4 changes: 2 additions & 2 deletions py4lo/callbacks/rewrite_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import xml.dom.minidom
from pathlib import Path
from typing import List
from typing import List, cast
from zipfile import ZipFile, ZipInfo

from callbacks.callback import ItemCallback
Expand Down Expand Up @@ -77,7 +77,7 @@ def _prettyfy_xml(zin: ZipFile, fname: str):

@staticmethod
def _strip_close(pretty_manifest: str):
lines = []
lines = cast(List[str], [])
for line in pretty_manifest.splitlines():
if line.strip() == MANIFEST_CLOSE_TAG: # end of manifest
return lines
Expand Down
6 changes: 3 additions & 3 deletions py4lo/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import Dict, List
from typing import Dict, List, cast

from commands.command import Command
from commands.command_executor import CommandExecutor
Expand Down Expand Up @@ -47,12 +47,12 @@ def get_help_message(self):
lines = [
"a command = {}".format(" | ".join(self._command_factory_by_name))]
for name, cf in self._command_factory_by_name.items():
lines.append("{}: {}".format(name, cf.get_help()))
lines.append("{}: {}".format(name, cf.get_help(None)))
return "\n".join(lines)


cf_by_name = real_command_factory_by_name.copy()
# add help now
cf_by_name['help'] = HelpCommand
cf_by_name['help'] = cast(Command, HelpCommand)

commands = Commands(cf_by_name)
8 changes: 4 additions & 4 deletions py4lo/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from abc import ABC, abstractmethod
from typing import List, Optional, Tuple, Any
from typing import List, Tuple, Any

from core.properties import PropertiesProvider

Expand All @@ -29,9 +29,9 @@ def create_executor(args: List[str], provider: PropertiesProvider
pass

@abstractmethod
def execute(self, *args: List[str]) -> Optional[Tuple]:
def execute(self, *args: List[str]) -> Tuple[Any, ...]:
pass

@staticmethod
def get_help():
@abstractmethod
def get_help(self) -> str:
pass
4 changes: 2 additions & 2 deletions py4lo/commands/command_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from logging import Logger
from typing import List, Tuple, Optional
from typing import List, Tuple, Optional, Any

from commands.command import Command

Expand All @@ -29,7 +29,7 @@ def __init__(self, logger: Logger, command: Command,
self._command = command
self._previous_executor = previous_executor

def execute(self, *args: List[str]) -> Optional[Tuple]:
def execute(self, *args: List[str]) -> Tuple[Any, ...]:
if self._previous_executor is None:
cur_args = []
else:
Expand Down
7 changes: 3 additions & 4 deletions py4lo/commands/debug_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import logging
from pathlib import Path
from typing import List, Tuple, Dict
from typing import List, Tuple, Dict, Any

import zip_updater
from callbacks import (IgnoreItem, RewriteManifest, AddScripts, AddAssets,
Expand Down Expand Up @@ -64,7 +64,7 @@ def __init__(self, logger: logging.Logger, helper: OdsUpdaterHelper,
self._debug_path = destinations.dest_ods_file.parent.joinpath(
doc_name)

def execute(self, *_args: List[str]) -> Tuple[Path]:
def execute(self, *_args: List[str]) -> Tuple[Any, ...]:
self._logger.info("Debug or init. Generating '%s' for Python '%s'",
self._debug_path, self._python_version)

Expand Down Expand Up @@ -96,6 +96,5 @@ def _get_zip_updater(self, assets: List[DestinationAsset],
zupdater = zupdater_builder.build()
return zupdater

@staticmethod
def get_help():
def get_help(self) -> str:
return "Create a debug.ods file with button for each function"
10 changes: 6 additions & 4 deletions py4lo/commands/help_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import Tuple, Any

from commands.command import Command
from commands.command_executor import CommandExecutor
from core.properties import PropertiesProvider
Expand Down Expand Up @@ -51,17 +53,17 @@ def __init__(self, command_factory_by_name, command_name=None):
self._command_factory_by_name = command_factory_by_name
self._command_name = command_name

def execute(self) -> None:
def execute(self) -> Tuple[Any, ...]:
if self._command_name:
try:
msg = self._command_factory_by_name[
self._command_name].get_help()
self._command_name].get_help(None)
except KeyError:
msg = self.get_help()
else:
msg = self.get_help()
print(msg)
return tuple()

@staticmethod
def get_help():
def get_help(self):
return "help [command]: Specific help message about command"
8 changes: 4 additions & 4 deletions py4lo/commands/init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import List, Tuple, Optional
from typing import List, Tuple, Any

from commands import Command
from commands.command_executor import CommandExecutor
Expand All @@ -42,9 +42,9 @@ def create_executor(args, provider: PropertiesProvider) -> CommandExecutor:
python_version, "new-project.ods")
return CommandExecutor(logger, init_command, test_executor)

def execute(self, *args: List[str]) -> Optional[Tuple]:
pass
def execute(self, *args: List[str]) -> Tuple[Any, ...]:
return tuple()

@staticmethod
def get_help():
def get_help(self) -> str:
return "Create a new document from script"
3 changes: 1 addition & 2 deletions py4lo/commands/null_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ def execute(self, *args: Any) -> Tuple[Any, ...]:
print(self._msg)
return tuple()

@staticmethod
def get_help() -> str:
def get_help(self) -> str:
return "<internal command>"
9 changes: 5 additions & 4 deletions py4lo/commands/real_command_factory_by_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import cast, Dict

from commands import Command
from commands.debug_command import DebugCommand
from commands.init_command import InitCommand
from commands.test_command import TestCommand
from commands.run_command import RunCommand
from commands.test_command import TestCommand
from commands.update_command import UpdateCommand


real_command_factory_by_name = {
real_command_factory_by_name = cast(Dict[str, Command], {
'debug': DebugCommand,
'init': InitCommand,
'test': TestCommand,
'run': RunCommand,
'update': UpdateCommand,
}
})
8 changes: 4 additions & 4 deletions py4lo/commands/run_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from pathlib import Path
from typing import cast
from typing import cast, Tuple, Any

from commands.command import Command
from commands.null_command import NullCommand
Expand All @@ -43,14 +43,14 @@ def create_executor(args, provider: PropertiesProvider) -> CommandExecutor:
def __init__(self, calc_exe: str):
self._calc_exe = calc_exe

def execute(self, status: int, dest_name: Path) -> None:
def execute(self, status: int, dest_name: Path) -> Tuple[Any, ...]:
if status == 0:
print("All tests ok")
logging.warning("%s %s", self._calc_exe, dest_name)
open_with_calc(dest_name, self._calc_exe)
else:
print("Error: some tests failed")
return tuple()

@staticmethod
def get_help():
def get_help(self) -> str:
return "Update + open the created file"
5 changes: 2 additions & 3 deletions py4lo/commands/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>."""
import os
import subprocess
import subprocess # nosec: B404
import sys
from logging import Logger
from pathlib import Path
Expand Down Expand Up @@ -115,6 +115,5 @@ def _get_env(self) -> Dict[str, str]:
self._logger.debug("PYTHONPATH = %s", env["PYTHONPATH"])
return self._env

@staticmethod
def get_help():
def get_help(self) -> str:
return "Do the test of the scripts to add to the spreadsheet"
7 changes: 3 additions & 4 deletions py4lo/commands/update_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from logging import Logger
from pathlib import Path
from typing import Optional, List
from typing import Optional, List, Tuple, Any

from callbacks import (AddReadmeWith, IgnoreItem, ARC_SCRIPTS_PATH,
RewriteManifest, AddScripts, AddAssets)
Expand Down Expand Up @@ -65,7 +65,7 @@ def __init__(self, logger: Logger, helper: OdsUpdaterHelper,
self._python_version = python_version
self._add_readme_callback = add_readme_callback

def execute(self, status: int) -> (int, Path):
def execute(self, status: int) -> Tuple[Any, ...]:
self._logger.info(
"Update. Generating '%s' (source: %s) for Python '%s'",
self._dest_ods_file,
Expand All @@ -92,6 +92,5 @@ def _create_updater(self, scripts: List[DestinationScript],

return zip_updater_builder.build()

@staticmethod
def get_help():
def get_help(self) -> str:
return "Update the file with all scripts"
2 changes: 1 addition & 1 deletion py4lo/core/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _get_destinations(self, source_ods_file: Path):
Path(dest["assets_dest_dir"]))
return destinations

def _get_dest_file(self, source_ods_file: Path) -> Path:
def _get_dest_file(self, source_ods_file: Optional[Path]) -> Path:
dest: Mapping[str, str] = self._tdata["dest"]
if "dest_ods_file" in dest:
dest_ods_file = Path(dest["dest_ods_file"])
Expand Down
4 changes: 2 additions & 2 deletions py4lo/core/source_dest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
import os
from dataclasses import dataclass
from pathlib import Path
from typing import List, Set
from typing import List, Set, Optional

from core.asset import SourceAsset, DestinationAsset
from core.script import SourceScript, TempScript, DestinationScript


@dataclass
class Sources:
source_ods_file: Path
source_ods_file: Optional[Path]
inc_dir: Path
lib_dir: Path
src_dir: Path
Expand Down
12 changes: 7 additions & 5 deletions py4lo/directive_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import shlex
from typing import List, Optional, Any
from typing import List, Optional, Any, Set, cast

from branch_processor import BranchProcessor
from comparator import Comparator
from core.script import SourceScript, TempScript
from directives import DirectiveProvider

PY4LO_REGEX = re.compile(r"^#\s*py4lo\s*:(.*)$")

class DirectiveProcessor:
"""A DirectiveProcessor processes directives, ie line that begins with #,
Expand Down Expand Up @@ -55,7 +57,7 @@ def __init__(self, script_set_processor: Any, # "ScriptSetProcessor",
self._script_set_processor = script_set_processor
self._branch_processor = branch_processor
self._directive_provider = directive_provider
self._includes = set()
self._includes = cast(Set[str], set())

def append_script(self, source_script: SourceScript):
"""Append a script to the script processor"""
Expand Down Expand Up @@ -108,9 +110,9 @@ def process_line(self) -> List[str]:
return self._target_lines

def _get_directive(self) -> Optional[List[str]]:
mark, directive = self._line.split(":", 1)
mark = mark.split()
if mark == ['#py4lo'] or mark == ['#', 'py4lo']:
m = PY4LO_REGEX.match(self._line)
if m:
directive = m.group(1)
return shlex.split(directive)
else:
return None
Expand Down
Loading

0 comments on commit 8786823

Please sign in to comment.