Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully fail with user-friendly error text when unrecognized datac… #2427

Merged
merged 6 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
housekeeping:
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4

- name: Set up Python
uses: actions/[email protected]
Expand Down Expand Up @@ -55,7 +55,7 @@ jobs:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4

- uses: actions/[email protected]
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/configlet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
- uses: actions/checkout@v2.3.4

- name: Fetch configlet
uses: exercism/github-actions/configlet-ci@main
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
test-runner:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2.3.4
- name: Run test-runner
run: docker-compose run test-runner
81 changes: 78 additions & 3 deletions bin/data.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
from enum import Enum
from dataclasses import dataclass, asdict
from dataclasses import dataclass, asdict, fields
import dataclasses
from itertools import chain
import json
from pathlib import Path
import toml
from typing import List, Any, Dict
from typing import List, Any, Dict, Type


def _custom_dataclass_init(self, *args, **kwargs):
# print(self.__class__.__name__, "__init__")
names = [field.name for field in fields(self)]
used_names = set()

# Handle positional arguments
for value in args:
try:
name = names.pop(0)
except IndexError:
raise TypeError(f"__init__() given too many positional arguments")
# print(f'setting {k}={v}')
setattr(self, name, value)
used_names.add(name)

# Handle keyword arguments
for name, value in kwargs.items():
if name in names:
# print(f'setting {k}={v}')
setattr(self, name, value)
used_names.add(name)
elif name in used_names:
raise TypeError(f"__init__() got multiple values for argument '{name}'")
else:
raise TypeError(
f"Unrecognized field '{name}' for dataclass {self.__class__.__name__}."
"\nIf this field is valid, please add it to the dataclass in data.py."
"\nIf adding an object-type field, please create a new dataclass for it."
)

# Check for missing positional arguments
missing = [
f"'{field.name}'" for field in fields(self)
if isinstance(field.default, dataclasses._MISSING_TYPE) and field.name not in used_names
]
if len(missing) == 1:
raise TypeError(f"__init__() missing 1 required positional argument: {missing[0]}")
elif len(missing) == 2:
raise TypeError(f"__init__() missing 2 required positional arguments: {' and '.join(missing)}")
elif len(missing) != 0:
missing[-1] = f"and {missing[-1]}"
raise TypeError(f"__init__() missing {len(missing)} required positional arguments: {', '.join(missing)}")

# Run post init if available
if hasattr(self, "__post_init__"):
self.__post_init__()


@dataclass
class TrackStatus:
__init__ = _custom_dataclass_init

concept_exercises: bool = False
test_runner: bool = False
representer: bool = False
Expand All @@ -27,12 +78,13 @@ class TestRunnerSettings:

@dataclass
class EditorSettings:
__init__ = _custom_dataclass_init

indent_style: IndentStyle = IndentStyle.Space
indent_size: int = 4
ace_editor_language: str = "python"
highlightjs_language: str = "python"


def __post_init__(self):
if isinstance(self.indent_style, str):
self.indent_style = IndentStyle(self.indent_style)
Expand All @@ -47,6 +99,8 @@ class ExerciseStatus(str, Enum):

@dataclass
class ExerciseFiles:
__init__ = _custom_dataclass_init

solution: List[str]
test: List[str]
exemplar: List[str] = None
Expand All @@ -71,6 +125,8 @@ def __post_init__(self):

@dataclass
class ExerciseConfig:
__init__ = _custom_dataclass_init

files: ExerciseFiles
authors: List[str] = None
forked_from: str = None
Expand All @@ -95,6 +151,8 @@ def load(cls, config_file: Path) -> "ExerciseConfig":

@dataclass
class ExerciseInfo:
__init__ = _custom_dataclass_init

path: Path
slug: str
name: str
Expand Down Expand Up @@ -160,6 +218,8 @@ def load_config(self) -> ExerciseConfig:

@dataclass
class Exercises:
__init__ = _custom_dataclass_init

concept: List[ExerciseInfo]
practice: List[ExerciseInfo]
foregone: List[str] = None
Expand Down Expand Up @@ -190,20 +250,26 @@ def all(self, status_filter={ExerciseStatus.Active, ExerciseStatus.Beta}):

@dataclass
class Concept:
__init__ = _custom_dataclass_init

uuid: str
slug: str
name: str


@dataclass
class Feature:
__init__ = _custom_dataclass_init

title: str
content: str
icon: str


@dataclass
class FilePatterns:
__init__ = _custom_dataclass_init

solution: List[str]
test: List[str]
example: List[str]
Expand All @@ -212,6 +278,8 @@ class FilePatterns:

@dataclass
class Config:
__init__ = _custom_dataclass_init

language: str
slug: str
active: bool
Expand Down Expand Up @@ -253,10 +321,15 @@ def load(cls, path="config.json"):
except IOError:
print(f"FAIL: {path} file not found")
raise SystemExit(1)
except TypeError as ex:
print(f"FAIL: {ex}")
raise SystemExit(1)


@dataclass
class TestCaseTOML:
__init__ = _custom_dataclass_init

uuid: str
description: str
include: bool = True
Expand All @@ -265,6 +338,8 @@ class TestCaseTOML:

@dataclass
class TestsTOML:
__init__ = _custom_dataclass_init

cases: Dict[str, TestCaseTOML]

@classmethod
Expand Down