-
Notifications
You must be signed in to change notification settings - Fork 261
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
Bump to v7.16.0 #457
base: master
Are you sure you want to change the base?
Bump to v7.16.0 #457
Conversation
- cdn/manager: add DataType class(enum), add optional parameter 'data_type' to `CdnManager.get_bandwidth_data` & `CdnManager.get_flux_data` - see https://jira.qiniu.io/browse/DP-5290
from qiniu.utils import io_md5, dt2ts | ||
|
||
from .endpoint import Endpoint | ||
from .region import Region, ServiceName | ||
from .default_client import qn_http_client | ||
from .middleware import RetryDomainsMiddleware | ||
from .single_flight import SingleFlight | ||
|
||
|
||
class RegionsProvider: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing class docstring (missing-class-docstring)
Details
lint 解释
Missing class docstring (missing-class-docstring)
是一个代码质量检查(lint)结果,表示在类定义中缺少文档字符串(docstring)。文档字符串是用于描述类、方法或函数的注释,它有助于提高代码的可读性和可维护性。
错误用法
以下是一个示例,展示了如何在类定义中缺少文档字符串:
class MyClass:
def my_method(self):
pass
在这个例子中,MyClass
类没有包含任何文档字符串。
正确用法
正确的做法是在类定义中添加文档字符串。以下是一个示例:
class MyClass:
"""
这是一个示例类。
该类用于演示如何在类定义中添加文档字符串。
"""
def my_method(self):
"""
这是一个示例方法。
该方法用于演示如何在方法定义中添加文档字符串。
"""
pass
在这个例子中,MyClass
类和 my_method
方法都包含了文档字符串。
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Internal class
from qiniu.utils import io_md5, dt2ts | ||
|
||
from .endpoint import Endpoint | ||
from .region import Region, ServiceName | ||
from .default_client import qn_http_client | ||
from .middleware import RetryDomainsMiddleware | ||
from .single_flight import SingleFlight | ||
|
||
|
||
class RegionsProvider: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too few public methods (1/2) (too-few-public-methods)
Details
lint 解释
too-few-public-methods
是一个代码质量检查规则,用于确保类中至少有一个公共方法。这个规则的目的是鼓励开发者编写具有实际功能的类,而不是空壳类。
错误用法
以下是一个错误的示例,展示了如何定义一个没有公共方法的类:
class MyClass:
def __init__(self):
pass
在这个例子中,MyClass
类只有一个私有构造函数 __init__
,没有任何公共方法。
正确用法
以下是一个正确的示例,展示了如何定义一个具有公共方法的类:
class MyClass:
def __init__(self):
pass
def my_method(self):
print("This is a public method")
在这个例子中,MyClass
类有一个私有构造函数 __init__
和一个公共方法 my_method
。
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
abc Generator
@@ -69,6 71,9 @@ def _get_region_from_query(data, **kwargs): | |||
) | |||
|
|||
|
|||
_query_regions_single_flight = SingleFlight() | |||
|
|||
|
|||
class QueryRegionsProvider(RegionsProvider): | |||
def __init__( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too many arguments (6/5) (too-many-arguments)
Details
lint 解释
这个lint结果表明在代码中定义的函数或方法接收了比预期更多的参数。具体来说,该函数或方法被定义为接受5个参数,但在实际调用时传递了6个参数。
错误用法
def my_function(a, b, c, d):
pass
my_function(1, 2, 3, 4, 5) # 这里传递了6个参数,超过了函数定义的参数数量
正确用法
def my_function(a, b, c, d):
pass
my_function(1, 2, 3, 4) # 这里传递了正确的参数数量
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
self.access_key, | ||
self.bucket_name | ||
]) | ||
regions = _query_regions_single_flight.do(flight_key, self.__fetch_regions) | ||
# change to `yield from` when min version of python update to >= 3.3 | ||
for r in regions: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use 'yield from' directly instead of yielding each element one by one (use-yield-from)
Details
lint 解释
use-yield-from
是一个代码质量检查工具(如 Pylint 或 Flake8)的规则,建议在生成器函数中使用 yield from
而不是逐个 yield 元素。这样可以提高代码的可读性和性能。
错误用法
以下是一个错误的示例,展示了如何逐个 yield 元素:
def generate_numbers(n):
for i in range(n):
yield i
正确用法
正确的做法是使用 yield from
来简化代码:
def generate_numbers(n):
yield from range(n)
通过使用 yield from
,代码更加简洁和易读。
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yield from
is not supported in python 2.7
@@ -137,27 150,136 @@ def __init__(self, message): | |||
super(FileAlreadyLocked, self).__init__(message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using Python 3 style super() without arguments (super-with-arguments)
Details
lint 解释
这个lint结果提示你使用Python 3的super()
函数时,应该不带参数调用(即super-with-arguments
)。在Python 2中,super()
需要两个参数:当前类和实例对象。而在Python 3中,super()
可以简化为不带参数的形式。
错误用法
以下是一个错误的示例代码:
class Parent:
def __init__(self, name):
self.name = name
class Child(Parent):
def __init__(self, name, age):
super(Child, self).__init__(name) # 错误用法
self.age = age
在这个示例中,super()
函数带了两个参数:当前类Child
和实例对象self
。
正确用法
以下是一个正确的示例代码:
class Parent:
def __init__(self, name):
self.name = name
class Child(Parent):
def __init__(self, name, age):
super().__init__(name) # 正确用法
self.age = age
在这个示例中,super()
函数不带参数,直接调用父类的构造方法。
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may run in python 2.7
with open(self.lock_file_path, 'w'): | ||
pass | ||
with _file_threading_lockers_lock: | ||
global _file_threading_lockers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using global for '_file_threading_lockers' but no assignment is done (global-variable-not-assigned)
Details
lint 解释
这个lint结果表明在代码中使用了全局变量 _file_threading_lockers
,但是没有对其进行赋值。这可能会导致变量未被正确初始化,从而引发潜在的错误。
错误用法
global _file_threading_lockers
正确用法
_file_threading_lockers = {}
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would never happen
threading_lock = _file_threading_lockers.get(self._file_path, threading.Lock()) | ||
# Could use keyword style `acquire(blocking=False)` when min version of python update to >= 3 | ||
if not threading_lock.acquire(False): | ||
raise FileAlreadyLocked('File {0} already locked'.format(self._file_path)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting a regular string which could be an f-string (consider-using-f-string)
Details
lint 解释
这个lint结果提示你在一个可以使用f-string的地方,却使用了普通的字符串格式化方法。f-string是一种更现代、更易读的字符串格式化方式。
错误用法
name = "Alice"
age = 30
message = "My name is " name " and I am " str(age) " years old."
正确用法
name = "Alice"
age = 30
message = f"My name is {name} and I am {age} years old."
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f-string
is not support in python < 3.6
fcntl.lockf(self._fd, fcntl.LOCK_EX | fcntl.LOCK_NB) | ||
except IOError: | ||
# Use `raise ... from ...` when min version of python update to >= 3 | ||
raise FileAlreadyLocked('File {0} already locked'.format(self._file_path)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider explicitly re-raising using 'except IOError as exc' and 'raise FileAlreadyLocked('File {0} already locked'.format(self._file_path)) from exc' (raise-missing-from)
Details
lint 解释
这个lint结果提示在捕获异常时,应该显式地重新抛出(re-raise)异常,并且使用from
关键字来保留原始异常的上下文信息。这样可以确保异常链的完整性,便于调试和理解。
错误用法
try:
# 一些可能引发IOError的操作
except IOError as exc:
raise FileAlreadyLocked('File {0} already locked'.format(self._file_path))
在这个错误示例中,捕获了IOError
异常后,直接重新抛出了一个新的FileAlreadyLocked
异常,但没有保留原始的IOError
异常信息。
正确用法
try:
# 一些可能引发IOError的操作
except IOError as exc:
raise FileAlreadyLocked('File {0} already locked'.format(self._file_path)) from exc
在这个正确示例中,捕获了IOError
异常后,使用from
关键字重新抛出了一个新的FileAlreadyLocked
异常,并保留了原始的IOError
异常信息。这样可以确保异常链的完整性,便于调试和理解。
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
raise ... from ...
is not supported in python 2.7
return self._fd.name | ||
|
||
elif is_windows: | ||
import msvcrt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unable to import 'msvcrt' (import-error)
Details
lint 解释
Unable to import 'msvcrt' (import-error)
这个lint结果表示在代码中尝试导入 msvcrt
模块时失败了。msvcrt
是一个特定于 Microsoft Visual C 的模块,通常用于 Windows 平台上的文件操作和控制台输入输出。如果在非 Windows 环境下尝试导入这个模块,就会导致错误。
错误用法
以下是一个示例代码,展示了如何错误地导入 msvcrt
模块:
import msvcrt # 错误的导入方式
正确用法
为了避免在非 Windows 环境下导入失败,可以使用条件导入来确保只有在 Windows 平台上才尝试导入 msvcrt
模块。以下是一个正确的示例:
import platform
if platform.system() == 'Windows':
import msvcrt # 只有在 Windows 上才导入
else:
# 提供替代方案或处理逻辑
pass
通过这种方式,可以确保代码在不同平台上都能正常运行。
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's in is_windows
block
|
||
def __enter__(self): | ||
try: | ||
self._lock_fd = open(self._lock_file_path, 'w') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using open without explicitly specifying an encoding (unspecified-encoding)
Details
lint 解释
在Python中,使用open()
函数打开文件时,默认情况下会以二进制模式('rb' 或 'wb')打开。如果需要读取或写入文本数据,应该显式指定编码格式,以避免潜在的字符编码问题。
错误用法
with open('example.txt') as file:
content = file.read()
在这个例子中,文件是以二进制模式打开的,因此无法正确读取文本内容。
正确用法
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
在这个例子中,文件是以文本模式('r')打开的,并且显式指定了编码格式为utf-8
,这样可以确保正确读取和处理文本内容。
💡 以上内容由 AI 辅助生成,如有疑问欢迎反馈交流
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just lock file. We don't care its encoding.
close #454, close #455, close #456