Import of typing should be conditional #178
Comments
- Fall back to `collections.namedtuple` for `Binding` when typing is not present - Add import guards to all imports - Fixes theskumar#178 Signed-off-by: Dan Ryan <dan@danryan.co>
|
We have indeed introduced a dependency on typing (as a separate package for Python < 3.5). This provides authors and users of Python-dotenv with static type checking.
Not necessary but idiomatic as far as I'm aware. However, I didn't know these imports could be slow or break. Do you have more information on that? I quite like static type checking so I would be sad if we had to go through more hoops to benefit from it (supporting Python 2 is already quite complicated). |
|
Sorry, actual evaluations are slow. Imports are just as slow as imports are — not incredibly bad but not inconsequential. Type hints are useful in the specific case of the The implementation is not all that common but we use it in a number of packaging authority libraries and I’ve been encouraging the practice more. It’s not too bad, I have a PR open at #179 with the changes. Happy to adjust if you have concerns |
|
Also I should mention static type checking is great and the changes should not break type checking for anyone on python 3.5 or above or with the typing package installed (currently anyone who installs this library) |
- Fall back to `collections.namedtuple` for `Binding` when typing is not present - Add import guards to all imports - Fixes theskumar#178 Signed-off-by: Dan Ryan <dan@danryan.co>
|
I've been thinking about this and I wondered why you would want this library to be more complex instead of vendoring
To avoid likely misconceptions:
|
The argument is more to persuade you that it is not a bad idea, and that there are valid reasons for putting an if statement around your typing imports. This does not help pipenv with performance, this helps pipenv from becoming broken where typing is not imported. It is unfortunately not actually possible to depend on vendored typing imports for this for a number of reasons or we would do that.
I'm not suggesting that it's the only useful type hit, it was just the only type hint that I had to put a fallback implementation around as a result of the changes I made which will be used in case the user doesn't have
Everyone who installs |
- Fall back to `collections.namedtuple` for `Binding` when typing is not present - Add import guards to all imports - Fixes #178 - Add missing coverage config to setup.cfg
Hey -- so we vendor this library over in pipenv and I recently updated to the latest release before realizing that there is now a hard dependency on
typing. We don't vendor typing and I'd rather avoid that, but it's also not strictly necessary to put unguardedtypingimports everywhere. They are slow, for one thing, or they can be, and they break on systems where typing isn't available.What I often do is include a utility method that sets a variable if the type checker is running, something like this (in
utils.pyorenvironment.py):If that solution makes sense here, I'm happy to PR it. I'll open a PR and feel free to merge or close as needed
The text was updated successfully, but these errors were encountered: