Skip to content

Commit

Permalink
Merge pull request #5 from pomponchik/develop
Browse files Browse the repository at this point in the history
0.0.8
  • Loading branch information
pomponchik authored Oct 9, 2023
2 parents cbc6e54 + 23f36a0 commit 9ebfcac
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ jobs:
- name: Run ruff
shell: bash
run: ruff f

- name: Run mypy
shell: bash
run: mypy f --strict --implicit-reexport
2 changes: 1 addition & 1 deletion .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[mypy]
disable_error_code = override
disable_error_code = override, return
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![Test-Package](https://github.com/pomponchik/fazy/actions/workflows/coverage.yml/badge.svg)](https://github.com/pomponchik/fazy/actions/workflows/coverage.yml)
[![PyPI version](https://badge.fury.io/py/fazy.svg)](https://badge.fury.io/py/fazy)
[![Python versions](https://img.shields.io/pypi/pyversions/fazy.svg)](https://pypi.python.org/pypi/fazy)
[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)


Expand Down
Binary file added docs/assets/logo_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 12 additions & 11 deletions f/lazy_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __getitem__(self, index: int) -> str:
def __getnewargs__(self) -> Tuple[List[ChainUnit], Dict[str, Any], Dict[str, Any], Dict[str, Any], bool]:
return (self.units, self.local_locals, self.local_globals, self.local_nonlocals, self.lazy)

def __mod__(self, args) -> str:
def __mod__(self, args: Union[str, Tuple[Any, ...]]) -> str:
if isinstance(args, type(self)):
args = args.data
return self.data.__mod__(args)
Expand Down Expand Up @@ -93,11 +93,12 @@ def translate(self, table: Dict[int, int]) -> str:
def title(self) -> str:
return self.data.title()

def startswith(self, prefix: Union['LazyString', str], *other_args: Union['LazyString', str]) -> bool:
def startswith(self, prefix: Union['LazyString', str, Tuple[Union['LazyString', str], ...]], *other_args: int) -> bool:
if isinstance(prefix, type(self)):
prefix = prefix.data
converted_other_args = [x if not isinstance(x, type(self)) else x.data for x in other_args]
return self.data.startswith(prefix, *converted_other_args)
elif isinstance(prefix, tuple):
prefix = tuple(*(x.data if isinstance(x, type(self)) else x for x in prefix))
return self.data.startswith(prefix, *other_args)

def endswith(self, suffix: Union['LazyString', str, Tuple[Union['LazyString', str], ...]], *other_args: int) -> bool:
if isinstance(suffix, type(self)):
Expand Down Expand Up @@ -153,13 +154,13 @@ def rstrip(self, chars: Optional[Union['LazyString', str]] = None) -> str:
chars = chars.data
return self.data.rstrip(chars)

def ljust(self, width, *args):
args = [item.data if isinstance(item, type(self)) else item for item in args]
return self.data.ljust(width, *args)
def ljust(self, width: int, *args: Union['LazyString', str]) -> str:
converted_args = [item.data if isinstance(item, type(self)) else item for item in args]
return self.data.ljust(width, *converted_args)

def rjust(self, width, *args):
args = [item.data if isinstance(item, type(self)) else item for item in args]
return self.data.rjust(width, *args)
def rjust(self, width: int, *args: Union['LazyString', str]) -> str:
converted_args = [item.data if isinstance(item, type(self)) else item for item in args]
return self.data.rjust(width, *converted_args)

def encode(self, **kwargs: Union['LazyString', str]) -> bytes:
kwargs = {key: value.data if isinstance(value, type(self)) else value for key, value in kwargs.items()}
Expand All @@ -179,7 +180,7 @@ def maketrans(x: Union[Dict[Union[int, str, UserString], Optional[Union[int, str
value = value.data if isinstance(value, UserString) else value
first_item[key] = value

return str.maketrans(first_item, *converted_others)
return str.maketrans(first_item, *converted_others) # type: ignore[arg-type]

def partition(self, sep: Union['LazyString', str]) -> Tuple[str, str, str]:
sep = sep.data if isinstance(sep, type(self)) else sep
Expand Down
22 changes: 13 additions & 9 deletions f/proxy_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import ast
import inspect
from string import Formatter
from types import CodeType
from typing import Iterable, Optional, Union, Sized, Dict, Callable, Any
from types import CodeType, FrameType
from typing import Iterable, Optional, Union, Sized, Dict, Callable, Type, Any
try:
from typing import Protocol
except ImportError:
from typing_extensions import Protocol # type: ignore[assignment]

from f.chain_unit import ChainUnit
from f.lazy_string import LazyString


class SizedAndIterable(Sized, Iterable[Any]):
class SizedAndIterable(Sized, Iterable[Any], Protocol):
pass

class ProxyModule(sys.modules[__name__].__class__):
class ProxyModule(sys.modules[__name__].__class__): # type: ignore[misc]
old_str = str

def __call__(self, string: Union[LazyString, str], lazy: bool = True, safe: bool = True, closures: bool = True) -> Union[LazyString, str]:
Expand All @@ -39,7 +43,7 @@ def __call__(self, string: Union[LazyString, str], lazy: bool = True, safe: bool
return result
return result.data

def sum_of_nonlocals(self, first_frame, base_qualname, closures, safe) -> Dict[str, Any]:
def sum_of_nonlocals(self, first_frame: Optional[FrameType], base_qualname: Optional[str], closures: bool, safe: bool) -> Dict[str, Any]:
if not closures or first_frame is None or base_qualname is None:
return {}

Expand All @@ -64,7 +68,7 @@ def sum_of_nonlocals(self, first_frame, base_qualname, closures, safe) -> Dict[s
return result

@classmethod
def get_qualname(cls: type, code: CodeType, raise_if_not_literal: bool, code_line: int) -> Optional[str]:
def get_qualname(cls: Type['ProxyModule'], code: CodeType, raise_if_not_literal: bool, code_line: int) -> Optional[str]:
functions = []

for function in gc.get_referrers(code):
Expand All @@ -80,7 +84,7 @@ def get_qualname(cls: type, code: CodeType, raise_if_not_literal: bool, code_lin
function = functions[0]
if raise_if_not_literal:
cls.check_code(function, code, code_line)
return function.__qualname__
return function.__qualname__ # type: ignore[no-any-return]

@staticmethod
def check_code(function: Callable[..., Any], code: CodeType, code_line: int) -> None:
Expand All @@ -103,9 +107,9 @@ def check_code(function: Callable[..., Any], code: CodeType, code_line: int) ->
full_code = ''.join(code_strings)
ast_of_code = ast.parse(full_code)

flag = True
flag: Union[int, bool] = True
class ConstantVisitor(ast.NodeVisitor):
def visit_Call(self, node) -> None:
def visit_Call(self, node: ast.Call) -> None:
nonlocal flag
if node.lineno + begin_code_line_number - 1 == code_line:
if hasattr(node.func, 'id') and node.func.id == 'f':
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import sys
from setuptools import setup, find_packages

with open('README.md', 'r', encoding='utf8') as readme_file:
readme = readme_file.read()

requirements = []
if sys.version_info < (3, 8):
requirements.append('typing-extensions==4.1.0')

setup(
name='fazy',
version='0.0.7',
version='0.0.8',
author='Evgeniy Blinov',
author_email='zheni-b@yandex.ru',
description='Lazy f-strings for everyone',
Expand Down

0 comments on commit 9ebfcac

Please sign in to comment.