Skip to content

Commit

Permalink
#166 replace .format() with f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ben05allen committed Jul 13, 2024
1 parent 832a4fa commit 6343afe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
20 changes: 13 additions & 7 deletions pygount/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def deprecated(reason: Optional[str]): # pragma: no cover
Decorator to mark functions as deprecated and log a warning in case it is called.
Source: https://stackoverflow.com/questions/2536307/decorators-in-the-python-standard-lib-deprecated-specifically
Updated replace .format() with f-string
"""

if isinstance(reason, str):
Expand All @@ -134,23 +136,23 @@ def deprecated(reason: Optional[str]): # pragma: no cover
# pass

def decorator(func1):
if inspect.isclass(func1):
fmt1 = "Call to deprecated class {name} ({reason})."
else:
fmt1 = "Call to deprecated function {name} ({reason})."
class_or_func = "class" if inspect.isclass(func1) else "function"

@functools.wraps(func1)
def new_func1(*args, **kwargs):
warnings.simplefilter("always", DeprecationWarning)
warnings.warn(
fmt1.format(name=func1.__name__, reason=reason), category=DeprecationWarning, stacklevel=2
f"Call to deprecated {class_or_func} {func1.__name__} ({reason}).",
category=DeprecationWarning,
stacklevel=2,
)
warnings.simplefilter("default", DeprecationWarning)
return func1(*args, **kwargs)

return new_func1

return decorator

if inspect.isclass(reason) or inspect.isfunction(reason):
# The @deprecated is used without any 'reason'.
#
Expand All @@ -161,12 +163,16 @@ def new_func1(*args, **kwargs):
# pass

func2 = reason
fmt2 = "Call to deprecated class {name}." if inspect.isclass(func2) else "Call to deprecated function {name}."
class_or_func = "class" if inspect.isclass(func2) else "function"

@functools.wraps(func2)
def new_func2(*args, **kwargs):
warnings.simplefilter("always", DeprecationWarning)
warnings.warn(fmt2.format(name=func2.__name__), category=DeprecationWarning, stacklevel=2)
warnings.warn(
f"Call to deprecated {class_or_func} {func2.__name__}.",
category=DeprecationWarning,
stacklevel=2,
)
warnings.simplefilter("default", DeprecationWarning)
return func2(*args, **kwargs)

Expand Down
6 changes: 3 additions & 3 deletions pygount/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ class LineWriter(BaseWriter):
def __init__(self, target_stream):
super().__init__(target_stream)
self.has_to_track_progress = False
self.template = "{0}\t{1}\t{2}\t{3}"

def add(self, source_analysis):
line_to_write = self.template.format(
source_analysis.code_count, source_analysis.language, source_analysis.group, source_analysis.path
line_to_write = (
f"{source_analysis.code_count}\t{source_analysis.language}\t"
f"{source_analysis.group}\t{source_analysis.path}"
)
self._target_stream.write(line_to_write + os.linesep)

Expand Down

0 comments on commit 6343afe

Please sign in to comment.