Skip to content

Commit 2d1f28c

Browse files
committed
fix: remove unnecessary UTF-8 encoding declarations and improve type hints across multiple files
1 parent 17a0d42 commit 2d1f28c

File tree

19 files changed

+74
-93
lines changed

19 files changed

+74
-93
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,21 @@ repos:
143143
files: \.pyi?$
144144
args: ["--skip=B101"] # skip assert-use warning
145145

146+
146147
# - repo: https://github.com/RobertCraigie/pyright-python
147148
# rev: v1.1.404
148149
# hooks:
149150
# - id: pyright
150151
# name: "🐍 pyright - Static type checker"
151152
# files: \.pyi?$
152153

153-
- repo: local
154-
hooks:
155-
- id: pyright
156-
name: "🐍 pyright (local) - Static type checker"
157-
language: system
158-
entry: "python -m pyright"
159-
files: \.pyi?$
154+
# - repo: local
155+
# hooks:
156+
# - id: pyright
157+
# name: "🐍 pyright (local) - Static type checker"
158+
# language: system
159+
# entry: "python -m pyright"
160+
# files: \.pyi?$
160161

161162
# --- Tests ---
162163
# - repo: local

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@
1010

1111

1212
**Full Changelog**: https://github.com/bybatkhuu/module-python-logging/compare/v6.0.0...v6.0.1
13-
14-

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ It is a `Loguru` based custom logging package for python projects.
3838
[OPTIONAL] For **DEVELOPMENT** environment:
3939

4040
- Install [**git**](https://git-scm.com/downloads)
41-
- Setup an [**SSH key**](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh) ([video tutorial](https://www.youtube.com/watch?v=snCP3c7wXw0))
41+
- Setup an [**SSH key**](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh)
4242

4343
### 2. 📥 Download or clone the repository
4444

@@ -151,7 +151,7 @@ logger.info("Logging info.")
151151

152152
### **Simple**
153153

154-
[**`configs/logger.yml`**](https://github.com/bybatkhuu/module-python-logging/blob/main/examples/simple/configs/logger.yml):
154+
[**`configs/logger.yml`**](./examples/simple/configs/logger.yml):
155155

156156
```yml
157157
logger:
@@ -164,9 +164,11 @@ logger:
164164
enabled: true
165165
```
166166
167-
[**`main.py`**](https://github.com/bybatkhuu/module-python-logging/blob/main/examples/simple/main.py):
167+
[**`main.py`**](./examples/simple/main.py):
168168

169169
```python
170+
#!/usr/bin/env python
171+
170172
from beans_logging.auto import logger
171173
172174
@@ -178,24 +180,27 @@ logger.warning("Warning something.")
178180
logger.error("Error occured.")
179181
logger.critical("CRITICAL ERROR.")
180182
183+
181184
def divide(a, b):
182185
_result = a / b
183186
return _result
184187
188+
185189
def nested(c):
186190
try:
187191
divide(5, c)
188192
except ZeroDivisionError as err:
189193
logger.error(err)
190194
raise
191195
196+
192197
try:
193198
nested(0)
194-
except Exception as err:
199+
except Exception:
195200
logger.exception("Show me, what value is wrong:")
196201
```
197202

198-
Run the [**`examples/simple`**](https://github.com/bybatkhuu/module-python-logging/tree/main/examples/simple):
203+
Run the [**`examples/simple`**](./examples/simple):
199204

200205
```sh
201206
cd ./examples/simple
@@ -241,7 +246,7 @@ ZeroDivisionError: division by zero
241246

242247
## ⚙️ Configuration
243248

244-
[**`templates/configs/config.yml`**](./templates/configs/config.yml):
249+
[**`templates/configs/logger.yml`**](./templates/configs/logger.yml):
245250

246251
```yaml
247252
logger:

docs/getting-started/configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ title: Configuration
44

55
# ⚙️ Configuration
66

7-
[**`templates/configs/config.yml`**](https://github.com/bybatkhuu/module-python-logging/blob/main/templates/configs/config.yml):
7+
[**`templates/configs/logger.yml`**](https://github.com/bybatkhuu/module-python-logging/blob/main/templates/configs/logger.yml):
88

99
```yaml
10-
--8<-- "./templates/configs/config.yml"
10+
--8<-- "./templates/configs/logger.yml"
1111
```
1212

1313
## 🌎 Environment Variables

docs/getting-started/prerequisites.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ title: Prerequisites
1212
[OPTIONAL] For **DEVELOPMENT** environment:
1313

1414
- Install [**git**](https://git-scm.com/downloads)
15-
- Setup an [**SSH key**](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh) ([video tutorial](https://www.youtube.com/watch?v=snCP3c7wXw0))
15+
- Setup an [**SSH key**](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh)

docs/release-notes.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ hide:
1616

1717

1818
**Full Changelog**: https://github.com/bybatkhuu/module-python-logging/compare/v6.0.0...v6.0.1
19-
20-

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
site_name: Python Logging (beans-logging)
2-
site_description: "'beans-logging' is a python package for simple logger and easily managing logging."
2+
site_description: "'beans-logging' is a python package for simple logger and easily managing logs."
33
site_url: https://pylogging-docs.bybatkhuu.dev
44
repo_name: bybatkhuu/module-python-logging
55
repo_url: https://github.com/bybatkhuu/module-python-logging

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
55
[project]
66
name = "beans_logging"
77
authors = [{ name = "Batkhuu Byambajav", email = "batkhuu10@gmail.com" }]
8-
description = "'beans-logging' is a python package for simple logger and easily managing logging."
8+
description = "'beans-logging' is a python package for simple logger and easily managing logs."
99
readme = "README.md"
1010
requires-python = ">=3.10,<4.0"
1111
keywords = [

src/beans_logging/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
from ._base import Logger, logger, LoggerLoader
42
from .schemas import LoggerConfigPM
53
from ._consts import WarnEnum

src/beans_logging/_base.py

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
# -*- coding: utf-8 -*-
2-
3-
## Standard libraries
1+
# Standard libraries
42
import os
53
import copy
64
import json
75
import logging
8-
from typing import Union, Dict, Any
6+
from typing import Any
97

10-
## Third-party libraries
8+
# Third-party libraries
119
import yaml
1210
from loguru import logger
1311
from loguru._logger import Logger
@@ -18,7 +16,7 @@
1816
else:
1917
from pydantic import validate_arguments as validate_call
2018

21-
## Internal modules
19+
# Internal modules
2220
from ._utils import create_dir, deep_merge
2321
from ._handlers import InterceptHandler
2422
from .rotation import RotationChecker
@@ -67,20 +65,22 @@ class LoggerLoader:
6765
@validate_call
6866
def __init__(
6967
self,
70-
config: Union[LoggerConfigPM, Dict[str, Any], None] = None,
68+
config: LoggerConfigPM | dict[str, Any] | None = None,
7169
config_file_path: str = _CONFIG_FILE_PATH,
7270
auto_config_file: bool = True,
7371
auto_load: bool = False,
7472
):
7573
"""LoggerLoader constructor method.
7674
7775
Args:
78-
config (Union[LoggerConfigPM,
79-
dict,
80-
None ], optional): New logger config to update loaded config. Defaults to None.
81-
config_file_path (str , optional): Logger config file path. Defaults to `LoggerLoader._CONFIG_FILE_PATH`.
82-
auto_config_file (bool , optional): Indicates whether to load logger config file or not. Defaults to True.
83-
auto_load (bool , optional): Indicates whether to load logger handlers or not. Defaults to False.
76+
config (LoggerConfigPM | dict | None], optional): New logger config to update loaded config.
77+
Defaults to None.
78+
config_file_path (str , optional): Logger config file path. Defaults to
79+
`LoggerLoader._CONFIG_FILE_PATH`.
80+
auto_config_file (bool , optional): Indicates whether to load logger config
81+
file or not. Defaults to True.
82+
auto_load (bool , optional): Indicates whether to load logger
83+
handlers or not. Defaults to False.
8484
"""
8585

8686
self.handlers_map = {"default": 0}
@@ -125,9 +125,7 @@ def load(self) -> Logger:
125125
return logger
126126

127127
@validate_call
128-
def remove_handler(
129-
self, handler: Union[str, None] = None, handler_type: str = "NAME"
130-
):
128+
def remove_handler(self, handler: str | None = None, handler_type: str = "NAME"):
131129
"""Remove all handlers or specific handler by name or id from logger.
132130
133131
Raises:
@@ -162,7 +160,7 @@ def remove_handler(
162160
self.handlers_map.clear()
163161

164162
@validate_call
165-
def update_config(self, config: Union[LoggerConfigPM, Dict[str, Any]]):
163+
def update_config(self, config: LoggerConfigPM | dict[str, Any]):
166164
"""Update logger config with new config.
167165
168166
Args:
@@ -213,13 +211,11 @@ def _load_config_file(self):
213211
# elif self.config_file_path.lower().endswith(".toml"):
214212
# _file_format = "TOML"
215213

216-
## Loading config from file, if it's exits:
214+
# Loading config from file, if it's exits:
217215
if os.path.isfile(self.config_file_path):
218216
if _file_format == "YAML":
219217
try:
220-
with open(
221-
self.config_file_path, "r", encoding="utf-8"
222-
) as _config_file:
218+
with open(self.config_file_path, encoding="utf-8") as _config_file:
223219
_new_config_dict = yaml.safe_load(_config_file) or {}
224220
if "logger" not in _new_config_dict:
225221
logger.warning(
@@ -242,9 +238,7 @@ def _load_config_file(self):
242238
raise
243239
elif _file_format == "JSON":
244240
try:
245-
with open(
246-
self.config_file_path, "r", encoding="utf-8"
247-
) as _config_file:
241+
with open(self.config_file_path, encoding="utf-8") as _config_file:
248242
_new_config_dict = json.load(_config_file) or {}
249243
if "logger" not in _new_config_dict:
250244
logger.warning(
@@ -296,7 +290,7 @@ def _load_config_file(self):
296290
def _check_env(self):
297291
"""Check environment variables for logger config."""
298292

299-
## Checking environment for DEBUG option:
293+
# Checking environment for DEBUG option:
300294
_is_debug = False
301295
_ENV = str(os.getenv("ENV")).strip().lower()
302296
_DEBUG = str(os.getenv("DEBUG")).strip().lower()
@@ -314,7 +308,7 @@ def _check_env(self):
314308
self.config.file.logs_dir = os.getenv("BEANS_LOGGING_LOGS_DIR")
315309

316310
# if self.config.stream.use_color:
317-
# ## Checking terminal could support xterm colors:
311+
# # Checking terminal could support xterm colors:
318312
# _TERM = str(os.getenv("TERM")).strip()
319313
# if not "xterm" in _TERM:
320314
# self.config.stream.use_color = False
@@ -536,7 +530,7 @@ def _load_intercept_handlers(self):
536530

537531
_intercept_handler = InterceptHandler()
538532

539-
## Intercepting all logs from standard (root logger) logging:
533+
# Intercepting all logs from standard (root logger) logging:
540534
logging.basicConfig(handlers=[_intercept_handler], level=0, force=True)
541535

542536
_intercepted_modules = set()
@@ -579,10 +573,10 @@ def _load_intercept_handlers(self):
579573
f"Intercepted modules: {list(_intercepted_modules)}; Muted modules: {list(_muted_modules)};"
580574
)
581575

582-
### ATTRIBUTES ###
583-
## handlers_map ##
576+
# ATTRIBUTES #
577+
# handlers_map
584578
@property
585-
def handlers_map(self) -> Dict[str, int]:
579+
def handlers_map(self) -> dict[str, int]:
586580
try:
587581
return self.__handlers_map
588582
except AttributeError:
@@ -591,17 +585,17 @@ def handlers_map(self) -> Dict[str, int]:
591585
return self.__handlers_map
592586

593587
@handlers_map.setter
594-
def handlers_map(self, handlers_map: Dict[str, int]):
588+
def handlers_map(self, handlers_map: dict[str, int]):
595589
if not isinstance(handlers_map, dict):
596590
raise TypeError(
597591
f"`handlers_map` attribute type {type(handlers_map)} is invalid, must be <dict>!."
598592
)
599593

600594
self.__handlers_map = copy.deepcopy(handlers_map)
601595

602-
## handlers_map ##
596+
# handlers_map
603597

604-
## config ##
598+
# config
605599
@property
606600
def config(self) -> LoggerConfigPM:
607601
try:
@@ -620,9 +614,9 @@ def config(self, config: LoggerConfigPM):
620614

621615
self.__config = copy.deepcopy(config)
622616

623-
## config ##
617+
# config
624618

625-
## config_file_path ##
619+
# config_file_path
626620
@property
627621
def config_file_path(self) -> str:
628622
try:
@@ -648,17 +642,19 @@ def config_file_path(self, config_file_path: str):
648642
):
649643
if not config_file_path.lower().endswith(".toml"):
650644
raise NotImplementedError(
651-
f"`config_file_path` attribute value '{config_file_path}' is invalid, TOML file format is not supported yet!"
645+
f"`config_file_path` attribute value '{config_file_path}' is invalid, "
646+
f"TOML file format is not supported yet!"
652647
)
653648

654649
raise ValueError(
655-
f"`config_file_path` attribute value '{config_file_path}' is invalid, file must be '.yml', '.yaml' or '.json' format!"
650+
f"`config_file_path` attribute value '{config_file_path}' is invalid, "
651+
f"file must be '.yml', '.yaml' or '.json' format!"
656652
)
657653

658654
if not os.path.isabs(config_file_path):
659655
config_file_path = os.path.join(os.getcwd(), config_file_path)
660656

661657
self.__config_file_path = config_file_path
662658

663-
## config_file_path ##
664-
### ATTRIBUTES ###
659+
# config_file_path
660+
# ATTRIBUTES #

0 commit comments

Comments
 (0)