Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
7c748f6815 | |||
8d5b30546e | |||
cebca481fc | |||
dd8cd1188e | |||
d30e73a679 |
@ -28,7 +28,7 @@ repos:
|
|||||||
hooks:
|
hooks:
|
||||||
- id: dead
|
- id: dead
|
||||||
- repo: https://github.com/asottile/pyupgrade
|
- repo: https://github.com/asottile/pyupgrade
|
||||||
rev: v3.15.0
|
rev: v3.15.1
|
||||||
hooks:
|
hooks:
|
||||||
- id: pyupgrade
|
- id: pyupgrade
|
||||||
args: [--py38-plus]
|
args: [--py38-plus]
|
||||||
@ -37,11 +37,11 @@ repos:
|
|||||||
hooks:
|
hooks:
|
||||||
- id: autopep8
|
- id: autopep8
|
||||||
- repo: https://github.com/PyCQA/flake8
|
- repo: https://github.com/PyCQA/flake8
|
||||||
rev: 6.1.0
|
rev: 7.0.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: flake8
|
- id: flake8
|
||||||
additional_dependencies: [flake8-encodings, flake8-warnings, flake8-builtins, flake8-length, flake8-print]
|
additional_dependencies: [flake8-encodings, flake8-warnings, flake8-builtins, flake8-length, flake8-print]
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v1.7.0
|
rev: v1.8.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
|
@ -13,6 +13,7 @@ from collections import defaultdict
|
|||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
from typing import cast
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import Generic
|
from typing import Generic
|
||||||
from typing import NoReturn
|
from typing import NoReturn
|
||||||
@ -84,6 +85,20 @@ else: # pragma: no cover
|
|||||||
removeprefix = str.removeprefix
|
removeprefix = str.removeprefix
|
||||||
|
|
||||||
|
|
||||||
|
def _isnamedtupleinstance(x: Any) -> bool:
|
||||||
|
t = type(x)
|
||||||
|
b = t.__bases__
|
||||||
|
|
||||||
|
if len(b) != 1 or b[0] != tuple:
|
||||||
|
return False
|
||||||
|
|
||||||
|
f = getattr(t, '_fields', None)
|
||||||
|
if not isinstance(f, tuple):
|
||||||
|
return False
|
||||||
|
|
||||||
|
return all(isinstance(n, str) for n in f)
|
||||||
|
|
||||||
|
|
||||||
class Setting:
|
class Setting:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -199,6 +214,11 @@ class Setting:
|
|||||||
return str
|
return str
|
||||||
else:
|
else:
|
||||||
if not self.cmdline and self.default is not None:
|
if not self.cmdline and self.default is not None:
|
||||||
|
if not isinstance(self.default, str) and not _isnamedtupleinstance(self.default) and isinstance(self.default, Sequence) and self.default and self.default[0]:
|
||||||
|
try:
|
||||||
|
return cast(type, type(self.default)[type(self.default[0])])
|
||||||
|
except Exception:
|
||||||
|
...
|
||||||
return type(self.default)
|
return type(self.default)
|
||||||
return 'Any'
|
return 'Any'
|
||||||
|
|
||||||
@ -211,6 +231,11 @@ class Setting:
|
|||||||
t: type | str = type_hints['return']
|
t: type | str = type_hints['return']
|
||||||
return t
|
return t
|
||||||
if self.default is not None:
|
if self.default is not None:
|
||||||
|
if not isinstance(self.default, str) and not _isnamedtupleinstance(self.default) and isinstance(self.default, Sequence) and self.default and self.default[0]:
|
||||||
|
try:
|
||||||
|
return cast(type, type(self.default)[type(self.default[0])])
|
||||||
|
except Exception:
|
||||||
|
...
|
||||||
return type(self.default)
|
return type(self.default)
|
||||||
return 'Any'
|
return 'Any'
|
||||||
|
|
||||||
@ -324,7 +349,9 @@ def generate_ns(definitions: Definitions) -> str:
|
|||||||
if type_name == 'Any':
|
if type_name == 'Any':
|
||||||
type_name = 'typing.Any'
|
type_name = 'typing.Any'
|
||||||
|
|
||||||
attributes.append(f' {setting.internal_name}: {type_name}')
|
attribute = f' {setting.internal_name}: {type_name}'
|
||||||
|
if attribute not in attributes:
|
||||||
|
attributes.append(attribute)
|
||||||
# Add a blank line between groups
|
# Add a blank line between groups
|
||||||
if attributes and attributes[-1] != '':
|
if attributes and attributes[-1] != '':
|
||||||
attributes.append('')
|
attributes.append('')
|
||||||
|
Reference in New Issue
Block a user