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

Run wheel build on PRs #3867

Closed
wants to merge 16 commits into from
12 changes: 8 additions & 4 deletions .github/workflows/pypi_upload.yml
@@ -1,8 +1,9 @@
name: Publish to PyPI
name: Build wheels and publish to PyPI

on:
release:
types: [published]
pull_request:

permissions:
contents: read
Expand All @@ -28,7 +29,8 @@ jobs:
- name: Build wheel and source distributions
run: python -m build

- name: Upload to PyPI via Twine
- if: github.event_name == 'release'
name: Upload to PyPI via Twine
env:
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: twine upload --verbose -u '__token__' dist/*
Expand Down Expand Up @@ -68,7 +70,8 @@ jobs:
name: ${{ matrix.name }}-mypyc-wheels
path: ./wheelhouse/*.whl

- name: Upload wheels to PyPI via Twine
- if: github.event_name == 'release'
name: Upload wheels to PyPI via Twine
env:
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: pipx run twine upload --verbose -u '__token__' wheelhouse/*.whl
Expand All @@ -87,7 +90,8 @@ jobs:
ref: stable
fetch-depth: 0

- name: Update stable branch to release tag & push
- if: github.event_name == 'release'
name: Update stable branch to release tag & push
run: |
git reset --hard ${{ github.event.release.tag_name }}
git push
19 changes: 10 additions & 9 deletions src/black/cache.py
Expand Up @@ -6,8 +6,9 @@
import tempfile
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, Iterable, NamedTuple, Set, Tuple
from typing import Dict, Iterable, Set, Tuple

from mypy_extensions import mypyc_attr
from platformdirs import user_cache_dir

from _black_version import version as __version__
Expand All @@ -19,10 +20,9 @@
from typing_extensions import Self


class FileData(NamedTuple):
st_mtime: float
st_size: int
hash: str
# We'd like to use a NamedTuple (st_mtime, st_size, hash) here, but
# that breaks mypyc.
FileData = Tuple[float, int, str]


def get_cache_dir() -> Path:
Expand All @@ -48,6 +48,7 @@ def get_cache_file(mode: Mode) -> Path:
return CACHE_DIR / f"cache.{mode.get_cache_key()}.pickle"


@mypyc_attr(patchable=True)
@dataclass
class Cache:
mode: Mode
Expand Down Expand Up @@ -86,7 +87,7 @@ def get_file_data(path: Path) -> FileData:

stat = path.stat()
hash = Cache.hash_digest(path)
return FileData(stat.st_mtime, stat.st_size, hash)
return (stat.st_mtime, stat.st_size, hash)

def is_changed(self, source: Path) -> bool:
"""Check if source has changed compared to cached version."""
Expand All @@ -96,11 +97,11 @@ def is_changed(self, source: Path) -> bool:
return True

st = res_src.stat()
if st.st_size != old.st_size:
if st.st_size != old[1]:
return True
if int(st.st_mtime) != int(old.st_mtime):
if int(st.st_mtime) != int(old[0]):
new_hash = Cache.hash_digest(res_src)
if new_hash != old.hash:
if new_hash != old[2]:
return True
return False

Expand Down
24 changes: 12 additions & 12 deletions tests/test_black.py
Expand Up @@ -2063,7 +2063,7 @@ def wrapped_func(path: Path) -> FileData:
if path == cached:
return orig_func(path)
if path == cached_but_changed:
return FileData(0.0, 0, "")
return (0.0, 0, "")
raise AssertionError

with patch.object(black.Cache, "get_file_data", side_effect=wrapped_func):
Expand All @@ -2085,30 +2085,30 @@ def test_filter_cached_hash(self) -> None:
todo, done = cache.filtered_cached([src])
assert todo == set()
assert done == {src}
assert cached_file_data.st_mtime == st.st_mtime
assert cached_file_data[0] == st.st_mtime

# Modify st_mtime
cached_file_data = cache.file_data[str(src)] = FileData(
cached_file_data.st_mtime - 1,
cached_file_data.st_size,
cached_file_data.hash,
cached_file_data = cache.file_data[str(src)] = (
cached_file_data[0] - 1,
cached_file_data[1],
cached_file_data[2],
)
todo, done = cache.filtered_cached([src])
assert todo == set()
assert done == {src}
assert cached_file_data.st_mtime < st.st_mtime
assert cached_file_data.st_size == st.st_size
assert cached_file_data.hash == black.Cache.hash_digest(src)
assert cached_file_data[0] < st.st_mtime
assert cached_file_data[1] == st.st_size
assert cached_file_data[2] == black.Cache.hash_digest(src)

# Modify contents
src.write_text("print('hello world')", encoding="utf-8")
new_st = src.stat()
todo, done = cache.filtered_cached([src])
assert todo == {src}
assert done == set()
assert cached_file_data.st_mtime < new_st.st_mtime
assert cached_file_data.st_size != new_st.st_size
assert cached_file_data.hash != black.Cache.hash_digest(src)
assert cached_file_data[0] < new_st.st_mtime
assert cached_file_data[1] != new_st.st_size
assert cached_file_data[2] != black.Cache.hash_digest(src)

def test_write_cache_creates_directory_if_needed(self) -> None:
mode = DEFAULT_MODE
Expand Down