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

Expose public class in type hints for dumps/loads (#130) #131

Closed
wants to merge 1 commit into from

Conversation

abravalheri
Copy link
Contributor

@abravalheri abravalheri commented May 25, 2021

Hello, this is a proposal to fix #130.

Unfortunately, I had lots of trouble trying to run the test suite locally (with both tox and poetry run pytest tests/), so sorry for advance if something goes wrong (what I ended up doing was firing up a python terminal, loading the test_api file and manually running test_mapping_types_can_be_dumped()/test_a_raw_dict_can_be_dumped()).

@@ -22,10 +23,10 @@
from .items import Whitespace
from .items import item
from .parser import Parser
from .toml_document import TOMLDocument as _TOMLDocument
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type hints of public functions (e.g. loads/dumps) where exposing something that looked a private class...
This makes it complicated to use the library in a typechecked project.

@@ -34,28 +35,28 @@ def loads(string): # type: (str) -> _TOMLDocument
return parse(string)


def dumps(data, sort_keys=False): # type: (_TOMLDocument, bool) -> str
def dumps(data, sort_keys=False): # type: (Mapping, bool) -> str
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapping should work as a common denominator of dict and TOMLDocument (TOMLDocument is an instance of Container that in turn is a MutableMapping), so the type hint should encompass the 2 possibilities.

I could have gone for MutableMapping, but I feel that the data being mutable is not something this function relies on (I might be wrong).

"""
Dumps a TOMLDocument into a string.
"""
if not isinstance(data, _TOMLDocument) and isinstance(data, dict):
data = item(data, _sort_keys=sort_keys)
if not isinstance(data, Container) and isinstance(data, Mapping):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed TOMLDocument to Container here, because I imagine that structures that are already internal to tomlkit are supposed to rely on as_string and that might be the case some of them are already Mappings (maybe TOML tables?).

The changes here are indeed making the function more acceptive in terms of arguments, e.g. MappingProxy (a Python idiom that makes it easy to pass "immutable dicts" around), OrderedDict (although probably the order will not be preserved for Python < 3.6). Alternatively we could also use Union[dict, TOMLDocument] to avoid augmenting the function's domain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Public API seems to rely on a private type (type hints)
2 participants