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

Support typing.Literal in python 3.8 #1027

Merged
merged 4 commits into from Nov 25, 2019

Conversation

dmontagu
Copy link
Contributor

@dmontagu dmontagu commented Nov 24, 2019

Change Summary

Attempt to use typing.Literal if available (e.g., with python 3.8).

Related issue number

Closes #1026

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)

pydantic/typing.py Outdated Show resolved Hide resolved
pydantic/typing.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Nov 25, 2019

Codecov Report

Merging #1027 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff           @@
##           master   #1027   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files          19      19           
  Lines        3299    3293    -6     
  Branches      651     651           
======================================
- Hits         3299    3293    -6
Impacted Files Coverage Δ
pydantic/utils.py 100% <ø> (ø) ⬆️
pydantic/fields.py 100% <ø> (ø) ⬆️
pydantic/typing.py 100% <100%> (ø) ⬆️
pydantic/validators.py 100% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 796b958...eb33de5. Read the comment docs.

except ImportError:
Literal = None # type: ignore

if sys.version_info < (3, 8):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needed to use the sys.version_info check for mypy compatibility with both 3.8 and <=3.7

if sys.version_info < (3, 8):
if TYPE_CHECKING:
from typing_extensions import Literal
else: # due to different mypy warnings raised during CI for python 3.7 and 3.8
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to add this gross if TYPE_CHECKING: block here because the Literal = None line was causing an "unnecessary ignore" warning during the 3.8 build. This approach seems fine to me here, but I'm open to alternatives.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a known issue in mypy with one developer commenting that --warn-unused-ignores is not recommended for use in CI. But barring that you can trick mypy into interpreting Literal as a Union with:

if sys.version_info < (3, 8):
    try:
        from typing_extensions import Literal as _Literal
    except ImportError:
        Literal = None
    else:
        Literal = _Literal
else:
    from typing import Literal

Copy link
Contributor Author

@dmontagu dmontagu Nov 25, 2019

Choose a reason for hiding this comment

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

Thanks for sharing the issue, and the workaround.

I definitely would prefer to keep that check on in CI though; otherwise it's too easy to accidentally forget, and then end up with confusion about what is being ignored and/or ignore a future error that should have been caught.

Copy link
Member

Choose a reason for hiding this comment

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

agreed, I hoped mypy would have a type: ignore flexible option, but couldn't find anything.

@dmontagu
Copy link
Contributor Author

dmontagu commented Nov 25, 2019

@samuelcolvin Modulo the remaining ugly if TYPE_CHECKING: block in the import of Literal, I think this is good to go. (Let me know if you have any suggestions for how to change it; I'm also fine with leaving it as is.)

@samuelcolvin samuelcolvin merged commit 6432669 into pydantic:master Nov 25, 2019
@samuelcolvin
Copy link
Member

awesome, thank you.

Bobronium pushed a commit to Bobronium/pydantic that referenced this pull request Nov 28, 2019
* Support typing.Literal in python 3.8

* Improve import pattern for Literal

* Update references to  in docs

* Try to get build to pass
andreshndz pushed a commit to cuenca-mx/pydantic that referenced this pull request Jan 17, 2020
* Support typing.Literal in python 3.8

* Improve import pattern for Literal

* Update references to  in docs

* Try to get build to pass
samuelcolvin added a commit that referenced this pull request Feb 27, 2020
* Refactor ._iter() method:
Moved all keys-related stuff (include, exclude, etc.) to ._iter()
Removed redundant iteration through default values
Almost all arguments checks moved out of loops, so checks happen once
Fast yield from .__dict__ on plain .iter() (x10 boost)
Removed redundant set(dict.keys()) in ._calculate_keys()

* Moved back from nested generator checks to checks, optimized copy a bit

* Bump pytest-mock from 1.12.0 to 1.12.1 (#1018)

Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 1.12.0 to 1.12.1.
- [Release notes](https://github.com/pytest-dev/pytest-mock/releases)
- [Changelog](https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst)
- [Commits](pytest-dev/pytest-mock@v1.12.0...v1.12.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* __str__ and __repr__ inheritance for models, fix #1022 (#1023)

* add testimonials section to docs with reference to python bytes podcast episode (#1025)

* add testimonials section with reference to python bytes podcast episode

* added description to changes directory

* Bump twine from 3.0.0 to 3.1.0 (#1029)

Bumps [twine](https://github.com/pypa/twine) from 3.0.0 to 3.1.0.
- [Release notes](https://github.com/pypa/twine/releases)
- [Changelog](https://github.com/pypa/twine/blob/master/docs/changelog.rst)
- [Commits](pypa/twine@3.0.0...3.1.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Support typing.Literal in python 3.8 (#1027)

* Support typing.Literal in python 3.8

* Improve import pattern for Literal

* Update references to  in docs

* Try to get build to pass

* Add support for mapping types as custom root (#958)

* Add support for mapping types as custom root

* Incorporate feedback

* Add changes

* Incorporate feedback

* Add docs and tests

* Fix linting issue

* Incorporate more feedback

* Add more specific match

* Add parse_as_type function (#934)

* Add parse_as_type function

* Add changes

* Incorporate feedback

* Add naming tests

* Fix double quotes

* Fix docs example

* Reorder parameters; add dataclass and mapping tests

* Rename parse_as_type to parse_obj, and add parse_file

* Incorporate feedback

* Incorporate feedback

* use custom root types

* Add better support for validator reuse (#941)

* Add better support for validator reuse

* Clean up classmethod unpacking

* Add changes

* Fix coverage check

* Make 3.8 compatible

* Update changes/940-dmontagu.md

Co-Authored-By: Samuel Colvin <s@muelcolvin.com>

* Make allow_reuse discoverable by adding to error message

* switch _check_validator_name to _prepare_validator

* Add changes file

* Delete unrelated files

* Add check that k in fields before using alias

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Remove redundant call to __iter__

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Use typing.AbstractSet

* Update pydantic/main.py

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Colin Sullivan <csullivan@brandwatch.com>
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this pull request Dec 23, 2023
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.

Cannot use Literal from standard library in 3.8
3 participants