Skip to content

Commit

Permalink
Feature/no import third party provided by stdlib (#1158)
Browse files Browse the repository at this point in the history
Remove dependency on importlib-metadata.
Remove dependency on tomli when using >= py311
  • Loading branch information
Spitfire1900 authored Oct 10, 2023
1 parent 9953ac9 commit 818ac2e
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 67 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
11 changes: 5 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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"
Expand All @@ -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*']
Expand Down
10 changes: 3 additions & 7 deletions third_party/yapf_third_party/_ylib2to3/pgen2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'

Expand Down
5 changes: 1 addition & 4 deletions yapf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions yapf/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.42.0'
18 changes: 5 additions & 13 deletions yapf/yapflib/file_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions yapf/yapflib/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 0 additions & 18 deletions yapftests/file_resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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()

Expand Down
9 changes: 0 additions & 9 deletions yapftests/style_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,13 @@ 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,
'Unable to find section'):
style.CreateStyleFromConfig(filepath)

def testPyprojectTomlParseYapfSection(self):
try:
import tomli # noqa: F401
except ImportError:
return

cfg = textwrap.dedent("""\
[tool.yapf]
Expand Down

0 comments on commit 818ac2e

Please sign in to comment.