Skip to content

Commit

Permalink
chore: remove all | from types to allow python 3.8/9 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Dec 16, 2023
1 parent e8f0959 commit 21e959b
Show file tree
Hide file tree
Showing 26 changed files with 187 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

- uses: actions/setup-python@v1
with:
python-version: "3.12"
python-version: "3.10"

- name: Install dependencies
run: |
Expand Down
12 changes: 7 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ jobs:
- "ubuntu-py38"
- "ubuntu-py39"
- "ubuntu-py310"
- "macos-py37"
- "ubuntu-py312"
- "macos-py38"
- "macos-py39"
- "macos-py310"
- "macos-py312"

include:
- name: "linting"
Expand All @@ -42,6 +44,10 @@ jobs:
python: "3.10"
os: ubuntu-latest
tox_env: "py310"
- name: "ubuntu-py312"
python: "3.12"
os: ubuntu-latest
tox_env: "py312"
- name: "macos-py38"
python: "3.8"
os: macos-latest
Expand All @@ -54,10 +60,6 @@ jobs:
python: "3.10"
os: macos-latest
tox_env: "py310"
- name: "macos-py311"
python: "3.11"
os: macos-latest
tox_env: "py311"
- name: "macos-py312"
python: "3.12"
os: macos-latest
Expand Down
14 changes: 14 additions & 0 deletions fuzz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Fuzzing

WIP

## Goals

- Find uncaught errors in the parser reducing overall bugs
- Generating corpus' of data to benchmark performance

### Resources

- [nlohmann/json fuzzing](https://github.com/nlohmann/json/blob/develop/tests/fuzzing.md)
- [hypothesis](https://github.com/HypothesisWorks/hypothesis)
- [docs](https://hypothesis.readthedocs.io/en/latest/)
11 changes: 11 additions & 0 deletions fuzz/corpus-gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
TEST_DATA_VERSION=3.1.0
wget https://github.com/nlohmann/json_test_data/archive/refs/tags/v$TEST_DATA_VERSION.zip
unzip v$TEST_DATA_VERSION.zip
rm v$TEST_DATA_VERSION.zip
for FORMAT in json bjdata bson cbor msgpack ubjson
do
rm -fr corpus_$FORMAT
mkdir corpus_$FORMAT
find json_test_data-$TEST_DATA_VERSION -size -5k -name "*.$FORMAT" -exec cp "{}" "corpus_$FORMAT" \;
done
rm -fr json_test_data-$TEST_DATA_VERSION
7 changes: 2 additions & 5 deletions hooks/model_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import sys
import os
import importlib
from typing import Type
from typing import Type, Optional
from pydantic import BaseModel, field_validator

from tackle import BaseHook, Context
Expand All @@ -14,7 +11,7 @@

class ModelOutput(BaseModel):
name: str
description: str | None
description: Optional[str]
type: str


Expand Down
1 change: 0 additions & 1 deletion hooks/provider_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import string
import importlib.machinery
import json
from types import UnionType

from pydantic import BaseModel, Field
import inspect
Expand Down
6 changes: 4 additions & 2 deletions providers/arithmatic/hooks/modulo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional, Union

from tackle import BaseHook, Field


Expand All @@ -18,15 +20,15 @@ class ModuloHook(BaseHook):
...,
description="The divisor to take the modulo with.",
)
equal_to: int | None = Field(
equal_to: Optional[int] = Field(
None,
description="Optional parameter to assert if the modulo is equal to. Returns a "
"bool then.",
)

args: list = ['input', 'divisor', 'equal_to']

def exec(self) -> int | bool:
def exec(self) -> Union[int, bool]:
if self.equal_to is not None:
return self.input % self.divisor == self.equal_to
return self.input % self.divisor
2 changes: 1 addition & 1 deletion providers/logic/hooks/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MatchHook(BaseHook):
"""

hook_name: str = 'match'
value: str | int | float | bool = Field(
value: Union[str, int, float, bool] = Field(
True,
render_by_default=True,
description="The value to match against. Defaults to boolean true so that "
Expand Down
10 changes: 7 additions & 3 deletions providers/paths/hooks/listdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from typing import Union

from tackle import BaseHook, Field
from tackle import BaseHook, Field, exceptions, Context


class ListdirHook(BaseHook):
Expand Down Expand Up @@ -30,8 +30,12 @@ class ListdirHook(BaseHook):
args: list = ['path']
_docs_order = 2

def exec(self) -> list:
files = os.listdir(os.path.expanduser(os.path.expandvars(self.path)))
@property
def exec(self, context: Context) -> list:
try:
files = os.listdir(os.path.expanduser(os.path.expandvars(self.path)))
except FileNotFoundError as e:
raise exceptions.HookCallException(f"FileNotFoundError - {e}", context)
if self.sort:
files.sort()
if self.ignore_hidden:
Expand Down
4 changes: 2 additions & 2 deletions providers/strings/hooks/strings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Union

from tackle import BaseHook, Field

Expand All @@ -23,7 +23,7 @@ class JoinHook(BaseHook):

hook_name: str = 'join'

input: List[str | int] = Field(
input: list[Union[str, int]] = Field(
..., description="A list of strings to join.", render_by_default=True
)
separator: str = Field('', description="String separator.")
Expand Down
8 changes: 5 additions & 3 deletions providers/tackle/hooks/import.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional, Union

from pydantic import BaseModel, Field, field_validator, ValidationInfo, ValidationError # noqa

from tackle.utils.command import unpack_args_kwargs_string
Expand All @@ -8,8 +10,8 @@
class RepoSource(BaseModel):
"""Repo object."""
src: str
version: str | None = None
latest: bool | None = None
version: Optional[str] = None
latest: Optional[bool] = None

@field_validator('latest')
def check_both_version_and_latest_defined(cls, v: bool, info: ValidationInfo):
Expand All @@ -27,7 +29,7 @@ class ImportHook(BaseHook):
"""

hook_name: str = 'import'
src: str | list = Field(
src: Union[str, list] = Field(
...,
description="A str reference to a source or a list of dicts with strings that "
"will be expanded with args (ie `foo --version latest`) or objects "
Expand Down
2 changes: 1 addition & 1 deletion providers/tackle/hooks/tackles.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TackleHook(BaseHook):
kwargs: str = 'override'
_docs_order = 0

def exec(self, context: Context) -> dict | list:
def exec(self, context: Context) -> Union[dict, list]:
if self.input_arg is None:
# If not given, assume we're looking for default tackle file in current dir
self.input_arg = '.'
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ freezegun
flake8
ptyprocess
requests
pytest-asyncio
52 changes: 27 additions & 25 deletions tackle/context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional, Union

from jinja2 import Environment, StrictUndefined
# from jinja2 import StrictUndefined
from jinja2.nativetypes import NativeEnvironment as Environment
Expand All @@ -11,30 +13,30 @@

@dataclass
class Source:
input_string: str | None = None
checkout: str | None = None
latest: bool | None = None
find_in_parent: bool | None = None
directory: str | None = None
file: str | None = None
base_dir: str | None = None
hooks_dir: str | None = None
name: str | None = None
raw: dict | list = None
input_string: Optional[str] = None
checkout: Optional[str] = None
latest: Optional[bool] = None
find_in_parent: Optional[bool] = None
directory: Optional[str] = None
file: Optional[str] = None
base_dir: Optional[str] = None
hooks_dir: Optional[str] = None
name: Optional[str] = None
raw: Union[dict, list] = None


@dataclass
class Data:
input: DocumentType | None = None
raw_input: DocumentType | None = None
pre_input: DocumentType | None = None
post_input: DocumentType | None = None
hooks_input: DocumentObjectType | None = None
public: DocumentType | None = None
private: DocumentType | None = None
temporary: DocumentType | None = None
existing: DocumentObjectType | None = None
overrides: DocumentObjectType | None = None
input: Optional[DocumentType] = None
raw_input: Optional[DocumentType] = None
pre_input: Optional[DocumentType] = None
post_input: Optional[DocumentType] = None
hooks_input: Optional[DocumentObjectType] = None
public: Optional[DocumentType] = None
private: Optional[DocumentType] = None
temporary: Optional[DocumentType] = None
existing: Optional[DocumentObjectType] = None
overrides: Optional[DocumentObjectType] = None


@dataclass
Expand Down Expand Up @@ -93,11 +95,11 @@ class Context:
key_path_block: list = None

# Internal data objects
input: InputArguments | None = None
source: Source | None = None
hooks: Hooks | None = None
data: Data | None = None
path: Paths | None = None
input: Optional[InputArguments] = None
source: Optional[Source] = None
hooks: Optional[Hooks] = None
data: Optional[Data] = None
path: Optional[Paths] = None

# Flag to denote to exit parsing
break_: bool = False
Expand Down
14 changes: 7 additions & 7 deletions tackle/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def create_hooks(
)


def extract_base_file(context: 'Context') -> list | dict:
def extract_base_file(context: 'Context') -> Union[list, dict]:
"""Read the tackle file and initialize input_context."""
if context.source.file:
try:
Expand Down Expand Up @@ -127,10 +127,10 @@ def get_overrides(
def new_data(
*,
context: 'Context',
raw_input: dict | list | None = None,
overrides: str | dict | None = None,
existing_data: str | dict | None = None,
_data: Data | None = None,
raw_input: Union[dict, list, None] = None,
overrides: Union[str, dict, None] = None,
existing_data: Union[str, dict, None] = None,
_data: Optional[Data] = None,
) -> Data:
"""
Create a data object which stores data as the source is parsed. When tackle is
Expand Down Expand Up @@ -521,9 +521,9 @@ def new_context(
hooks_dir: str = None,
_strict_source: bool = False, # Raise if source not found
# Data
raw_input: dict | list | None = None,
raw_input: Union[dict, list, None] = None,
overrides: Union[str, dict] = None,
existing_data: str | dict | None = None,
existing_data: Union[str, dict, None] = None,
# Context
no_input: bool = None,
verbose: bool = None,
Expand Down
12 changes: 6 additions & 6 deletions tackle/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def get_hook_field_type_from_str(
context: Context,
hook_name: str,
type_str: str,
) -> GenericFieldType | None:
) -> Optional[GenericFieldType]:
"""
Get the type from a field's type string and raise an error if the type is unknown.
Supports the following types:
Expand Down Expand Up @@ -406,7 +406,7 @@ def create_hook_field_validator(
hook_name: str,
key: str,
value: dict,
validator_field: str | dict,
validator_field: Union[str, dict],
) -> GenericFieldType:
"""
Creates a validator by parsing a hook fields `validator` key and does some light
Expand Down Expand Up @@ -630,7 +630,7 @@ def create_dcl_hook_fields(
def new_dcl_hook_input(
context: 'Context',
hook_name: str,
hook_input_dict: dict | str,
hook_input_dict: Union[dict, str],
) -> DclHookInput:
"""
Create the hook_input which are keys supplied in the hook definition that inform how
Expand Down Expand Up @@ -659,7 +659,7 @@ def get_model_config_from_hook_input(
context: Context,
hook_name: str,
hook_input_dict: dict,
) -> DclHookModelConfig | None:
) -> Optional[DclHookModelConfig]:
"""If the `model_config` field is declared, pop it off and return an object."""
if 'model_config' in hook_input_dict:
model_config = hook_input_dict.pop('model_config')
Expand Down Expand Up @@ -697,7 +697,7 @@ def get_model_config_from_hook_input(
def create_dcl_hook(
context: 'Context',
hook_name: str,
hook_input_raw: dict | str,
hook_input_raw: Union[dict, str],
) -> 'Type[BaseHook]':
"""
Create a model from the hook input dict. Calls numerous functions to upgrade the
Expand Down Expand Up @@ -857,7 +857,7 @@ def enrich_hook(
def get_hooks_from_namespace(
context: 'Context',
hook_name: str,
) -> CompiledHookType | None:
) -> Optional[CompiledHookType]:
"""Get the public, private, or native hook from the context."""
if hook_name == '_default':
return context.hooks.default
Expand Down
Loading

0 comments on commit 21e959b

Please sign in to comment.