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

chore: Use TypedDict for more precise **kwargs typing #765

Merged
merged 1 commit into from
Mar 18, 2023
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
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def mypy(session: Session) -> None:
session.install(
"mypy",
"pytest",
"sphinx",
"types-docutils",
"types-requests",
)
Expand Down
38 changes: 10 additions & 28 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ omit = [
show_missing = true

[tool.mypy]
enable_incomplete_feature = "Unpack"
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true
Expand Down Expand Up @@ -85,7 +86,7 @@ version = "0.5.0"
importlib_metadata = {version = ">=1.6", python = "<3.8"}
python = "^3.7.0"
requests = "^2.23.0"
typing-extensions = {version = "^4.4.0", python = "<3.8"}
typing-extensions = {version = "^4.4.0", python = "<3.12"}

# Docs
furo = {version = "^2022.12.7", optional = true}
Expand Down
16 changes: 11 additions & 5 deletions src/citric/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from types import TracebackType
from typing import Any, BinaryIO, Generator, Iterable, Mapping, Sequence

from typing_extensions import Unpack

from citric import types

if sys.version_info >= (3, 8):
Expand Down Expand Up @@ -1231,7 +1233,11 @@ def list_survey_groups(
"""
return self.__session.list_survey_groups(username)

def set_group_properties(self, group_id: int, **properties: Any) -> dict[str, bool]:
def set_group_properties(
self,
group_id: int,
**properties: Unpack[types.GroupProperties],
) -> dict[str, bool]:
"""Set properties of a group.

Args:
Expand All @@ -1247,7 +1253,7 @@ def set_language_properties(
self,
survey_id: int,
language: str | None = None,
**properties: Any,
**properties: Unpack[types.LanguageProperties],
) -> dict[str, Any]:
"""Set properties of a survey language.

Expand Down Expand Up @@ -1288,7 +1294,7 @@ def set_question_properties(
self,
question_id: int,
language: str | None = None,
**properties: Any,
**properties: Unpack[types.QuestionProperties],
) -> dict[str, bool]:
"""Set properties of a question.

Expand All @@ -1305,7 +1311,7 @@ def set_question_properties(
def set_quota_properties(
self,
quota_id: int,
**properties: Any,
**properties: Unpack[types.QuotaProperties],
) -> types.SetQuotaPropertiesResult:
"""Set properties of a quota.

Expand All @@ -1325,7 +1331,7 @@ def set_quota_properties(
def set_survey_properties(
self,
survey_id: int,
**properties: Any,
**properties: Unpack[types.SurveyProperties],
) -> dict[str, bool]:
"""Set properties of a survey.

Expand Down
10 changes: 5 additions & 5 deletions src/citric/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FileUploadResult(TypedDict):
msg: str


class GroupProperties(TypedDict):
class GroupProperties(TypedDict, total=False):
"""Group properties.

Keys:
Expand All @@ -59,7 +59,7 @@ class GroupProperties(TypedDict):
description: str


class LanguageProperties(TypedDict):
class LanguageProperties(TypedDict, total=False):
"""Language properties.

Keys:
Expand Down Expand Up @@ -197,7 +197,7 @@ class QuestionsListElement(TypedDict):
same_script: int


class QuestionProperties(TypedDict):
class QuestionProperties(TypedDict, total=False):
"""Question properties result.

Keys:
Expand Down Expand Up @@ -278,7 +278,7 @@ class QuotaListElement(TypedDict):
autoload_url: int


class QuotaProperties(TypedDict):
class QuotaProperties(TypedDict, total=False):
"""Quota properties result.

Keys:
Expand Down Expand Up @@ -326,7 +326,7 @@ class SetQuotaPropertiesResult(TypedDict):
message: QuotaProperties


class SurveyProperties(TypedDict):
class SurveyProperties(TypedDict, total=False):
"""Survey properties result.

Keys:
Expand Down
15 changes: 10 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from _pytest._py.path import LocalPath

from citric import types

NEW_SURVEY_NAME = "New Survey"

Expand Down Expand Up @@ -493,7 +494,7 @@ def test_download_files(client: MockClient, tmp_path: Path):

def test_set_group_properties(client: MockClient):
"""Test set_group_properties client method."""
props = {"name": "foo"}
props: types.GroupProperties = {"group_name": "foo"}
assert client.set_group_properties(
1,
**props,
Expand All @@ -502,7 +503,7 @@ def test_set_group_properties(client: MockClient):

def test_set_language_properties(client: MockClient):
"""Test set_language_properties client method."""
props = {"name": "foo"}
props: types.LanguageProperties = {"surveyls_title": "foo"}
assert client.set_language_properties(
1,
language="en",
Expand All @@ -522,7 +523,7 @@ def test_set_participant_properties(client: MockClient):

def test_set_question_properties(client: MockClient):
"""Test set_question_properties client method."""
props = {"title": "foo", "question": "bar", "type": "text"}
props: types.QuestionProperties = {"title": "foo", "mandatory": "Y", "type": "text"}
assert client.set_question_properties(
1,
language="en",
Expand All @@ -532,7 +533,7 @@ def test_set_question_properties(client: MockClient):

def test_set_quota_properties(client: MockClient):
"""Test set_quota_properties client method."""
props = {"name": "foo", "quota": "bar", "action": "baz"}
props: types.QuotaProperties = {"name": "foo", "qlimit": 150, "action": 1}
assert client.set_quota_properties(
1,
**props,
Expand All @@ -541,7 +542,11 @@ def test_set_quota_properties(client: MockClient):

def test_set_survey_properties(client: MockClient):
"""Test set_survey_properties client method."""
props = {"title": "foo", "welcome": "bar", "end": "baz"}
props: types.SurveyProperties = {
"allowsave": "Y",
"ipanonymize": "Y",
"emailnotificationto": "[email protected]",
}
assert client.set_survey_properties(
1,
**props,
Expand Down