diff --git a/CHANGELOG.md b/CHANGELOG.md index 3daffcc6b..14f671fb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ # All notable changes to this project will be documented in this file. # This project adheres to [Semantic Versioning](http://semver.org/). +## (0.40.3) UNRELEASED +### Changes +- Remove dependency on importlib-metadata +- Remove dependency on tomli when using >= py311 + + ## [0.40.2] 2023-09-22 ### Changes - The verification module has been removed. NOTE: this changes the public APIs diff --git a/HACKING.md b/HACKING.md index 336b234bd..1c03e8008 100644 --- a/HACKING.md +++ b/HACKING.md @@ -3,13 +3,13 @@ - To run YAPF on all of YAPF: ```bash -$ PYTHONPATH=$PWD/yapf python -m yapf -i -r . +$ pipx run --spec=${PWD} --no-cache yapf -m -i -r yapf/ yapftests/ third_party/ ``` - To run YAPF on just the files changed in the current git branch: ```bash -$ PYTHONPATH=$PWD/yapf python -m yapf -i $(git diff --name-only @{upstream}) +$ pipx run --spec=${PWD} --no-cache yapf -m -i $(git diff --name-only @{upstream}) ``` ## Testing and building redistributables locally @@ -45,7 +45,7 @@ $ pipx run --spec='tox<4' tox -e bdist_wheel -e sdist $ pipx run --spec='tox<4' tox ``` -1. Bump version in `pyproject.toml`. +1. Bump version in `yapf/_version.py`. 1. Build and test redistributables diff --git a/pyproject.toml b/pyproject.toml index 04519ae24..0b4bde36c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,10 +7,10 @@ name = "yapf" description = "A formatter for Python code" authors = [{ name = "Google Inc." }] maintainers = [{ name = "Bill Wendling", email = "morbo@google.com" }] +dynamic = ["version"] license = { file = "LICENSE" } readme = "README.md" requires-python = ">=3.7" -version = "0.40.2" classifiers = [ 'Development Status :: 4 - Beta', 'Environment :: Console', @@ -27,11 +27,7 @@ classifiers = [ 'Topic :: Software Development :: Libraries :: Python Modules', 'Topic :: Software Development :: Quality Assurance', ] -dependencies = [ - 'importlib-metadata>=6.6.0', - 'platformdirs>=3.5.1', - 'tomli>=2.0.1', -] +dependencies = ['platformdirs>=3.5.1', 'tomli>=2.0.1; python_version<"3.11"'] [project.scripts] yapf = "yapf:run_main" @@ -51,6 +47,9 @@ python_tag = "py3" include-package-data = true package-dir = { yapf_third_party = 'third_party/yapf_third_party' } +[tool.setuptools.dynamic] +version = { attr = "yapf._version.__version__" } + [tool.setuptools.packages.find] where = [".", 'third_party'] include = ["yapf*", 'yapftests*'] diff --git a/third_party/yapf_third_party/_ylib2to3/pgen2/driver.py b/third_party/yapf_third_party/_ylib2to3/pgen2/driver.py index 9345fe5aa..76b31a11c 100644 --- a/third_party/yapf_third_party/_ylib2to3/pgen2/driver.py +++ b/third_party/yapf_third_party/_ylib2to3/pgen2/driver.py @@ -20,20 +20,19 @@ import pkgutil import sys # Python imports -from configparser import ConfigParser from contextlib import contextmanager from dataclasses import dataclass from dataclasses import field from pathlib import Path -from pkgutil import get_data from typing import Any from typing import Iterator from typing import List from typing import Optional -from importlib_metadata import metadata from platformdirs import user_cache_dir +from yapf._version import __version__ as yapf_version + # Pgen imports from . import grammar from . import parse @@ -207,10 +206,7 @@ def _generate_pickle_name(gt): if tail == '.txt': tail = '' cache_dir = user_cache_dir( - appname=metadata('yapf')['Name'].upper(), - appauthor=metadata('yapf')['Author'].split(' ')[0], - version=metadata('yapf')['Version'], - ) + appname='YAPF', appauthor='Google', version=yapf_version) return cache_dir + os.sep + head + tail + '-py' + '.'.join( map(str, sys.version_info)) + '.pickle' diff --git a/yapf/__init__.py b/yapf/__init__.py index ebbc75862..cf4be9379 100644 --- a/yapf/__init__.py +++ b/yapf/__init__.py @@ -33,15 +33,12 @@ import os import sys -from importlib_metadata import metadata - +from yapf._version import __version__ from yapf.yapflib import errors from yapf.yapflib import file_resources from yapf.yapflib import style from yapf.yapflib import yapf_api -__version__ = metadata('yapf')['Version'] - def _raw_input(): wrapper = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') diff --git a/yapf/_version.py b/yapf/_version.py new file mode 100644 index 000000000..ccd8b38ef --- /dev/null +++ b/yapf/_version.py @@ -0,0 +1 @@ +__version__ = '0.42.0' diff --git a/yapf/yapflib/file_resources.py b/yapf/yapflib/file_resources.py index 977a2568f..66ab707ac 100644 --- a/yapf/yapflib/file_resources.py +++ b/yapf/yapflib/file_resources.py @@ -28,6 +28,11 @@ from yapf.yapflib import errors from yapf.yapflib import style +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib + CR = '\r' LF = '\n' CRLF = '\r\n' @@ -51,12 +56,6 @@ def _GetExcludePatternsFromYapfIgnore(filename): def _GetExcludePatternsFromPyprojectToml(filename): """Get a list of file patterns to ignore from pyproject.toml.""" ignore_patterns = [] - try: - import tomli as tomllib - except ImportError: - raise errors.YapfError( - 'tomli package is needed for using pyproject.toml as a ' - 'configuration file') if os.path.isfile(filename) and os.access(filename, os.R_OK): with open(filename, 'rb') as fd: @@ -136,13 +135,6 @@ def GetDefaultStyleForDir(dirname, default_style=style.DEFAULT_STYLE): pass # It's okay if it's not there. else: with fd: - try: - import tomli as tomllib - except ImportError: - raise errors.YapfError( - 'tomli package is needed for using pyproject.toml as a ' - 'configuration file') - pyproject_toml = tomllib.load(fd) style_dict = pyproject_toml.get('tool', {}).get('yapf', None) if style_dict is not None: diff --git a/yapf/yapflib/style.py b/yapf/yapflib/style.py index 72f277b25..b43437ee0 100644 --- a/yapf/yapflib/style.py +++ b/yapf/yapflib/style.py @@ -15,11 +15,17 @@ import os import re +import sys import textwrap from configparser import ConfigParser from yapf.yapflib import errors +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib + class StyleConfigError(errors.YapfError): """Raised when there's a problem reading the style configuration.""" @@ -791,13 +797,6 @@ def _CreateConfigParserFromConfigFile(config_filename): config = ConfigParser() if config_filename.endswith(PYPROJECT_TOML): - try: - import tomli as tomllib - except ImportError: - raise errors.YapfError( - 'tomli package is needed for using pyproject.toml as a ' - 'configuration file') - with open(config_filename, 'rb') as style_file: pyproject_toml = tomllib.load(style_file) style_dict = pyproject_toml.get('tool', {}).get('yapf', None) diff --git a/yapftests/file_resources_test.py b/yapftests/file_resources_test.py index c55333f9d..e71742c6a 100644 --- a/yapftests/file_resources_test.py +++ b/yapftests/file_resources_test.py @@ -76,10 +76,6 @@ def test_get_exclude_file_patterns_from_yapfignore_with_wrong_syntax(self): file_resources.GetExcludePatternsForDir(self.test_tmpdir) def test_get_exclude_file_patterns_from_pyproject(self): - try: - import tomli - except ImportError: - return local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml') ignore_patterns = ['temp/**/*.py', 'temp2/*.py'] with open(local_ignore_file, 'w') as f: @@ -93,10 +89,6 @@ def test_get_exclude_file_patterns_from_pyproject(self): sorted(ignore_patterns)) def test_get_exclude_file_patterns_from_pyproject_no_ignore_section(self): - try: - import tomli - except ImportError: - return local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml') ignore_patterns = [] open(local_ignore_file, 'w').close() @@ -106,10 +98,6 @@ def test_get_exclude_file_patterns_from_pyproject_no_ignore_section(self): sorted(ignore_patterns)) def test_get_exclude_file_patterns_from_pyproject_ignore_section_empty(self): - try: - import tomli - except ImportError: - return local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml') ignore_patterns = [] with open(local_ignore_file, 'w') as f: @@ -175,12 +163,6 @@ def test_setup_config(self): file_resources.GetDefaultStyleForDir(test_dir)) def test_pyproject_toml(self): - # An empty pyproject.toml file should not be used - try: - import tomli - except ImportError: - return - pyproject_toml = os.path.join(self.test_tmpdir, 'pyproject.toml') open(pyproject_toml, 'w').close() diff --git a/yapftests/style_test.py b/yapftests/style_test.py index 10c62edc6..64e64a5be 100644 --- a/yapftests/style_test.py +++ b/yapftests/style_test.py @@ -229,11 +229,6 @@ def testErrorUnknownStyleOption(self): style.CreateStyleFromConfig(filepath) def testPyprojectTomlNoYapfSection(self): - try: - import tomli # noqa: F401 - except ImportError: - return - filepath = os.path.join(self.test_tmpdir, 'pyproject.toml') _ = open(filepath, 'w') with self.assertRaisesRegex(style.StyleConfigError, @@ -241,10 +236,6 @@ def testPyprojectTomlNoYapfSection(self): style.CreateStyleFromConfig(filepath) def testPyprojectTomlParseYapfSection(self): - try: - import tomli # noqa: F401 - except ImportError: - return cfg = textwrap.dedent("""\ [tool.yapf]