-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
Conversation
@@ -22,10 +23,10 @@ | |||
from .items import Whitespace | |||
from .items import item | |||
from .parser import Parser | |||
from .toml_document import TOMLDocument as _TOMLDocument |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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 Mapping
s (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.
Hello, this is a proposal to fix #130.
Unfortunately, I had lots of trouble trying to run the test suite locally (with both
tox
andpoetry run pytest tests/
), so sorry for advance if something goes wrong (what I ended up doing was firing up a python terminal, loading thetest_api
file and manually runningtest_mapping_types_can_be_dumped()/test_a_raw_dict_can_be_dumped()
).