Skip to content

Issue and test deprecation warnings #1886

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

Merged
merged 89 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
2382891
Test that deprecated Diff.renamed property warns
EliahKagan Mar 27, 2024
e7dec7d
Have the deprecated Diff.renamed property issue a warning
EliahKagan Mar 27, 2024
a8f109c
Fix exception in Popen.__del__ in test on Windows
EliahKagan Mar 28, 2024
fffa6ce
Test that the preferred renamed_file property does not warn
EliahKagan Mar 28, 2024
bc111b7
Add a TODO for simplifying the single_diff fixture
EliahKagan Mar 28, 2024
e3728c3
Decompose new fixture logic better
EliahKagan Mar 28, 2024
ff4b58d
Extract no-deprecation-warning asserter as a context manager
EliahKagan Mar 28, 2024
2c52696
Test that the deprecated Commit.trailers property warns
EliahKagan Mar 28, 2024
03464d9
Have the deprecated Commit.trailers property issue a warning
EliahKagan Mar 28, 2024
9d096e0
Test that Traversable.{list_,}traverse, but not overrides, warn
EliahKagan Mar 28, 2024
21c2b72
Use the :exc: Sphinx role for DeprecationWarning
EliahKagan Mar 28, 2024
ca385a5
Test that subclassing deprecated git.util.Iterable warns
EliahKagan Mar 28, 2024
8bbcb26
Call repo.close() instead of manually collecting
EliahKagan Mar 28, 2024
b8ce990
Better name and document the basic deprecation test module
EliahKagan Mar 28, 2024
61273aa
Annotate basic deprecation tests; have mypy scan it
EliahKagan Mar 29, 2024
b7a3d8c
Start on top-level module attribute access regression tests
EliahKagan Mar 19, 2024
105f500
Test attribute access and importing separately
EliahKagan Mar 19, 2024
859e38c
Expand to test top-level deprecated names
EliahKagan Mar 21, 2024
46a739d
Hoist `import git` to module level in test module
EliahKagan Mar 21, 2024
a2df3a8
Test static typing of private module aliases
EliahKagan Mar 21, 2024
a15a830
Improve a couple test case docstrings
EliahKagan Mar 21, 2024
dbaa535
Add a couple missing assert keywords
EliahKagan Mar 21, 2024
d00c843
Clarify how test_private_module_aliases is statically checkable
EliahKagan Mar 21, 2024
983fda7
Move mark-sharing tests into a class
EliahKagan Mar 21, 2024
19acd4c
Add FIXME for what to do next
EliahKagan Mar 22, 2024
f39bbb5
Fix a test docstring
EliahKagan Mar 22, 2024
aee7078
Test resolution into git.index.util using git.util
EliahKagan Mar 22, 2024
7f4a191
Fix brittle way of checking warning messages
EliahKagan Mar 22, 2024
d08a576
Clarify todo
EliahKagan Mar 22, 2024
9d58e6d
Start reorganizing new tests more in the GitPython style
EliahKagan Mar 23, 2024
45c128b
Finish reorganizing; fix assertion for duplicated messages
EliahKagan Mar 23, 2024
247dc15
Add imports so pyright recognizes refs and index
EliahKagan Mar 23, 2024
b05963c
Expand and clarify test module docstring
EliahKagan Mar 23, 2024
074bbc7
Tiny import tweak
EliahKagan Mar 23, 2024
18608e4
Pick a better name for _MODULE_ALIAS_TARGETS
EliahKagan Mar 23, 2024
1f290f1
Use typing_extensions only if needed
EliahKagan Mar 23, 2024
7a4f7eb
Fix zip calls
EliahKagan Mar 23, 2024
5977a6e
Fix (and improve wording) of docstrings
EliahKagan Mar 23, 2024
5b1fa58
Remove extra import "from typing_extensions"
EliahKagan Mar 23, 2024
a07be0e
Start on test_compat
EliahKagan Mar 23, 2024
d4917d0
Expand to test all three is_<platform> aliases
EliahKagan Mar 23, 2024
f4e5f42
Slightly improve docstrings
EliahKagan Mar 23, 2024
d54f851
Add test of dir() on git.compat
EliahKagan Mar 23, 2024
aaf046a
Add static type assertions to is_platform test
EliahKagan Mar 23, 2024
84d734d
Refactor test_compat.test_dir for clarity
EliahKagan Mar 23, 2024
3a621b3
Add top-level dir() tests
EliahKagan Mar 23, 2024
05e0878
Remove old comment meant as todo (that was done)
EliahKagan Mar 23, 2024
3fe2f15
Test that top-level aliases point to modules with normal __name__
EliahKagan Mar 23, 2024
246cc17
Use names directly on other tests
EliahKagan Mar 23, 2024
d7b6b31
Fix a small docstring typo
EliahKagan Mar 23, 2024
96089c8
Improve description in test module docstrings
EliahKagan Mar 23, 2024
a0ef537
Start on test_types
EliahKagan Mar 24, 2024
52e7360
Explain substring assertions in test_toplevel
EliahKagan Mar 24, 2024
e3675a0
Expand Lit_commit_ish test name and write docstring
EliahKagan Mar 24, 2024
4857ff0
Clarify test_compat.test_dir
EliahKagan Mar 24, 2024
488cc13
Add test of dir() on git.types
EliahKagan Mar 24, 2024
19b3c08
Clarify comment about is_<platform> value assertions
EliahKagan Mar 24, 2024
28bd4a3
Issue warnings for some deprecated attributes of modules
EliahKagan Mar 20, 2024
dffa930
Refine deprecated module attributes and their warnings
EliahKagan Mar 24, 2024
7ab27c5
Start on test module about Git.USE_SHELL and Git attributes
EliahKagan Mar 24, 2024
af723d5
Make test_use_shell_on_class more robust
EliahKagan Mar 25, 2024
bf13888
Write most remaining Git attribute/deprecation tests
EliahKagan Mar 25, 2024
602de0c
Begin multiprocessing misadventure
EliahKagan Mar 25, 2024
d4b50c9
Somewhat clarify multiprocessing misadventure
EliahKagan Mar 25, 2024
02c2f00
Discuss multiprocessing in test module docstring; remove bad test
EliahKagan Mar 26, 2024
46df79f
Discuss metaclass conflicts in test module docstring
EliahKagan Mar 26, 2024
40ed842
Revise test module docstring for clarity
EliahKagan Mar 26, 2024
6a35261
Test that USE_SHELL is unittest.mock.patch patchable
EliahKagan Mar 27, 2024
e725c82
Make the restore_use_shell_state fixture more robust
EliahKagan Mar 27, 2024
436bcaa
Add `type: ignore` in test that we can't set USE_SHELL on instances
EliahKagan Mar 27, 2024
febda6f
Clarify unittest.mock.patch patchability test docstring
EliahKagan Mar 27, 2024
4037108
Test that Git.execute's own read of USE_SHELL does not warn
EliahKagan Mar 27, 2024
0e311bf
Suppress type errors in restore_use_shell_state _USE_SHELL branches
EliahKagan Mar 27, 2024
df4c5c0
Fix wrong/unclear grammar in test_instance_dir docstring
EliahKagan Mar 28, 2024
d38e721
Issue warnings whenever Git.USE_SHELL is accessed
EliahKagan Mar 27, 2024
05de5c0
Implement instance USE_SHELL lookup in __getattr__
EliahKagan Mar 27, 2024
04eb09c
Have USE_SHELL warn but work like normal via super()
EliahKagan Mar 27, 2024
c6f518b
Keep mypy from thinking Git has arbitrary class attributes
EliahKagan Mar 27, 2024
c5d5b16
Clarify that the private name mangling is intentional
EliahKagan Mar 27, 2024
84bf2ca
Read USE_SHELL in Git.execute without DeprecationWarning
EliahKagan Mar 27, 2024
5bef7ed
Add GitPython project top comments to new test modules
EliahKagan Mar 29, 2024
3da47c2
Hide `del util` from type checkers
EliahKagan Mar 29, 2024
bdabb21
Expand USE_SHELL docstring; clarify a test usage
EliahKagan Mar 29, 2024
b51b080
Explain the approach in test.deprecation to static checking
EliahKagan Mar 29, 2024
7cd3aa9
Make test.performance.lib docstring more specific
EliahKagan Mar 30, 2024
cf2576e
Make/use test.deprecation.lib; abandon idea to filter by module
EliahKagan Mar 30, 2024
f92f4c3
Clarify security risk in USE_SHELL doc and warnings
EliahKagan Mar 31, 2024
8327b45
Test GitMeta alias
EliahKagan Mar 27, 2024
f6060df
Add GitMeta alias
EliahKagan Mar 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Finish reorganizing; fix assertion for duplicated messages
To support the changes, this adds typing-extensions as a test
dependency, since we are now importing from it in a test module.
But this should probably be required and used conditionally based
on whether the Python version has assert_type in its typing module.
  • Loading branch information
EliahKagan committed Mar 29, 2024
commit 45c128bcd82cd278d7926425179503c22ce1271c
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pytest-cov
pytest-instafail
pytest-mock
pytest-sugar
typing-extensions
98 changes: 49 additions & 49 deletions test/deprecation/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,72 @@
checks static typing of the code under test. (Running pytest checks dynamic behavior.)
"""

import importlib
from itertools import groupby
from typing import Type

import pytest
from typing_extensions import assert_type

import git


def test_cannot_get_undefined() -> None:
def test_cannot_access_undefined() -> None:
"""Accessing a bogus attribute in git remains both a dynamic and static error."""
with pytest.raises(AttributeError):
git.foo # type: ignore[attr-defined]


def test_cannot_import_undefined() -> None:
"""Importing a bogus attribute from git remains both a dynamic and static error."""
with pytest.raises(ImportError):
from git import foo # type: ignore[attr-defined] # noqa: F401


def test_util_alias_members_resolve() -> None:
"""git.index.util members can be accessed via git.util, and mypy recognizes it."""
gu_tfs = git.util.TemporaryFileSwap
from git.index.util import TemporaryFileSwap
def test_util_alias_access() -> None:
"""Accessing util in git works, warns, and mypy verifies it and its attributes."""
# The attribute access should succeed.
with pytest.deprecated_call() as ctx:
util = git.util

def accepts_tfs_type(t: Type[TemporaryFileSwap]) -> None:
pass
# There should be exactly one warning and it should have our util-specific message.
(message,) = [str(entry.message) for entry in ctx]
assert "git.util" in message
assert "git.index.util" in message
assert "should not be relied on" in message

def rejects_tfs_type(t: Type[git.Git]) -> None:
pass
# We check access through the util alias to the TemporaryFileSwap member, since it
# is slightly simpler to validate and reason about than the other public members,
# which are functions (specifically, higher-order functions for use as decorators).
from git.index.util import TemporaryFileSwap

# TODO: When typing_extensions is made a test dependency, use assert_type for this.
accepts_tfs_type(gu_tfs)
rejects_tfs_type(gu_tfs) # type: ignore[arg-type]
assert_type(util.TemporaryFileSwap, Type[TemporaryFileSwap])

assert gu_tfs is TemporaryFileSwap
# This comes after the static assertion, just in case it would affect the inference.
assert util.TemporaryFileSwap is TemporaryFileSwap


def test_util_alias_access_warns() -> None:
def test_util_alias_import() -> None:
"""Importing util from git works, warns, and mypy verifies it and its attributes."""
# The import should succeed.
with pytest.deprecated_call() as ctx:
git.util
from git import util

assert len(ctx) == 1
message = str(ctx[0].message)
# There may be multiple warnings. In CPython there will be currently always be
# exactly two, possibly due to the equivalent of calling hasattr to do a pre-check
# prior to retrieving the attribute for actual use. However, all warnings should
# have the same message, and it should be our util-specific message.
(message,) = {str(entry.message) for entry in ctx}
assert "git.util" in message
assert "git.index.util" in message
assert "should not be relied on" in message

# As above, we check access through the util alias to the TemporaryFileSwap member.
from git.index.util import TemporaryFileSwap

def test_util_alias_import_warns() -> None:
with pytest.deprecated_call() as ctx:
from git import util # noqa: F401
assert_type(util.TemporaryFileSwap, Type[TemporaryFileSwap])

message = str(ctx[0].message)
assert "git.util" in message
assert "git.index.util" in message
assert "should not be relied on" in message
# This comes after the static assertion, just in case it would affect the inference.
assert util.TemporaryFileSwap is TemporaryFileSwap


# Split out util and have all its tests be separate, above.
Expand All @@ -71,12 +82,11 @@ def test_util_alias_import_warns() -> None:
git.index.base,
git.index.fun,
git.index.typ,
git.index.util,
)


def test_private_module_alias_access_on_git_module() -> None:
"""Private alias access works, warns, and except for util is a mypy error."""
def test_private_module_alias_access() -> None:
"""Non-util private alias access works, warns, but is a deliberate mypy error."""
with pytest.deprecated_call() as ctx:
assert (
git.head, # type: ignore[attr-defined]
Expand All @@ -87,21 +97,16 @@ def test_private_module_alias_access_on_git_module() -> None:
git.base, # type: ignore[attr-defined]
git.fun, # type: ignore[attr-defined]
git.typ, # type: ignore[attr-defined]
git.util,
) == _MODULE_ALIAS_TARGETS

# Each should have warned exactly once, and note what to use instead.
messages = [str(w.message) for w in ctx]
for target, message in zip(_MODULE_ALIAS_TARGETS[:-1], messages[:-1], strict=True):
for target, message in zip(_MODULE_ALIAS_TARGETS, messages, strict=True):
assert message.endswith(f"Use {target.__name__} instead.")

util_message = messages[-1]
assert "git.util" in util_message
assert "git.index.util" in util_message
assert "should not be relied on" in util_message


def test_private_module_alias_import_from_git_module() -> None:
"""Private alias import works, warns, and except for util is a mypy error."""
def test_private_module_alias_import() -> None:
"""Non-util private alias access works, warns, but is a deliberate mypy error."""
with pytest.deprecated_call() as ctx:
from git import head # type: ignore[attr-defined]
from git import log # type: ignore[attr-defined]
Expand All @@ -111,7 +116,6 @@ def test_private_module_alias_import_from_git_module() -> None:
from git import base # type: ignore[attr-defined]
from git import fun # type: ignore[attr-defined]
from git import typ # type: ignore[attr-defined]
from git import util

assert (
head,
Expand All @@ -122,17 +126,13 @@ def test_private_module_alias_import_from_git_module() -> None:
base,
fun,
typ,
util,
) == _MODULE_ALIAS_TARGETS

# FIXME: This fails because, with imports, multiple consecutive accesses may occur.
# In practice, with CPython, it is always exactly two accesses, the first from the
# equivalent of a hasattr, and the second to fetch the attribute intentionally.
messages = [str(w.message) for w in ctx]
for target, message in zip(_MODULE_ALIAS_TARGETS[:-1], messages[:-1], strict=True):
# Each import may warn multiple times. In CPython there will be currently always be
# exactly two warnings per import, possibly due to the equivalent of calling hasattr
# to do a pre-check prior to retrieving the attribute for actual use. However, for
# each import, all messages should be the same and should note what to use instead.
messages_with_duplicates = [str(w.message) for w in ctx]
messages = [message for message, _ in groupby(messages_with_duplicates)]
for target, message in zip(_MODULE_ALIAS_TARGETS, messages, strict=True):
assert message.endswith(f"Use {target.__name__} instead.")

util_message = messages[-1]
assert "git.util" in util_message
assert "git.index.util" in util_message
assert "should not be relied on" in util_message