Initial commit: 项目初始化

This commit is contained in:
python 2025-12-04 14:41:20 +08:00
commit 2b6b76f694
2018 changed files with 333820 additions and 0 deletions

14
.env Normal file
View File

@ -0,0 +1,14 @@
# 硅基流动API配置
SILICONFLOW_API_KEY=sk-xnhmtotmlpjomrejbwdbczbpbyvanpxndvbxltodjwzbpmni
SILICONFLOW_MODEL=deepseek-ai/DeepSeek-V3.2-Exp
# 华为大模型API配置预留
HUAWEI_API_ENDPOINT=
HUAWEI_API_KEY=
# 数据库配置
DB_HOST=152.136.177.240
DB_PORT=5012
DB_USER=finyx
DB_PASSWORD=6QsGK6MpePZDE57Z
DB_NAME=finyx

14
.env.example Normal file
View File

@ -0,0 +1,14 @@
# 硅基流动API配置
SILICONFLOW_API_KEY=your_api_key_here
SILICONFLOW_MODEL=deepseek-ai/DeepSeek-V3.2-Exp
# 华为大模型API配置预留
HUAWEI_API_ENDPOINT=
HUAWEI_API_KEY=
# 数据库配置
DB_HOST=152.136.177.240
DB_PORT=5012
DB_USER=finyx
DB_PASSWORD=6QsGK6MpePZDE57Z
DB_NAME=finyx

257
README.md Normal file
View File

@ -0,0 +1,257 @@
# 智慧监督AI文书写作服务
基于大模型的智能文书生成服务,支持从非结构化文本中提取结构化字段数据。
## 功能特性
- ✅ AI解析接口 (`/api/ai/extract`) - 从输入文本中提取结构化字段
- ✅ 字段配置管理 - 从数据库读取字段配置
- ✅ 支持硅基流动大模型DeepSeek
- 🔄 预留华为大模型接口支持
- ✅ Web测试界面 - 可视化测试解析功能
## 项目结构
```
.
├── app.py # Flask主应用
├── requirements.txt # Python依赖
├── .env.example # 环境变量配置示例
├── services/ # 服务层
│ ├── ai_service.py # AI服务大模型调用
│ └── field_service.py # 字段服务(数据库操作)
├── utils/ # 工具类
│ └── response.py # 响应格式化
└── static/ # 静态文件
└── index.html # 测试页面
```
## 快速开始
### 1. 环境准备
**Windows系统**
```bash
# 运行安装脚本
setup_env.bat
```
**Linux/Mac系统**
```bash
# 运行安装脚本
chmod +x setup_env.sh
./setup_env.sh
```
**手动安装:**
```bash
# 创建虚拟环境
python -m venv venv
# 激活虚拟环境
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# 安装依赖
pip install -r requirements.txt
```
### 2. 配置环境变量
复制 `.env.example``.env` 并填入配置:
```bash
# Windows:
copy .env.example .env
# Linux/Mac:
cp .env.example .env
```
编辑 `.env` 文件填入你的API密钥
```env
# 硅基流动API配置必需
SILICONFLOW_API_KEY=your_api_key_here
SILICONFLOW_MODEL=deepseek-ai/DeepSeek-V3.2-Exp
# 数据库配置(已默认配置,如需修改可调整)
DB_HOST=152.136.177.240
DB_PORT=5012
DB_USER=finyx
DB_PASSWORD=6QsGK6MpePZDE57Z
DB_NAME=finyx
```
### 3. 启动服务
```bash
# 确保虚拟环境已激活
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# 启动服务
python app.py
```
服务启动后,访问:
- **测试页面**: http://localhost:7500/
- **API接口**: http://localhost:7500/api/ai/extract
## API接口说明
### 解析接口
**接口地址**: `POST /api/ai/extract`
**请求参数**:
```json
{
"businessType": "INVESTIGATION",
"inputData": [
{
"fieldCode": "clue_info",
"fieldValue": "被举报用户名称是张三年龄30岁"
},
{
"fieldCode": "target_basic_info_clue",
"fieldValue": "张三汉族1980年5月出生山西太原人"
}
]
}
```
**响应格式**:
```json
{
"code": 0,
"data": {
"outData": [
{
"fieldCode": "target_name",
"fieldValue": "张三"
},
{
"fieldCode": "target_gender",
"fieldValue": "男"
}
]
},
"msg": "ok",
"timestamp": "1764204337101",
"errorMsg": "",
"isSuccess": true
}
```
### 获取字段配置接口
**接口地址**: `GET /api/fields?businessType=INVESTIGATION`
**响应格式**:
```json
{
"code": 0,
"data": {
"input_fields": [...],
"output_fields": [...]
},
"isSuccess": true
}
```
## 测试方法
### 方法1: 使用Web测试页面
1. 启动服务后,在浏览器访问 `http://localhost:7500/`
2. 在"输入数据"区域填写输入字段
3. 点击"开始解析"按钮
4. 查看解析结果
### 方法2: 使用curl命令
```bash
curl -X POST http://localhost:7500/api/ai/extract \
-H "Content-Type: application/json" \
-d '{
"businessType": "INVESTIGATION",
"inputData": [
{
"fieldCode": "clue_info",
"fieldValue": "被举报用户名称是张三年龄30岁某公司总经理"
}
]
}'
```
### 方法3: 使用Python脚本
```python
import requests
url = "http://localhost:7500/api/ai/extract"
data = {
"businessType": "INVESTIGATION",
"inputData": [
{
"fieldCode": "clue_info",
"fieldValue": "被举报用户名称是张三年龄30岁"
}
]
}
response = requests.post(url, json=data)
print(response.json())
```
## 错误码说明
| 错误码 | 说明 | 处理建议 |
|--------|------|----------|
| 0 | 成功 | - |
| 400 | 请求参数错误 | 检查请求参数格式 |
| 1001 | 模板不存在 | 检查businessType是否正确 |
| 2001 | AI解析超时 | 重新尝试解析 |
| 2002 | 字段识别失败 | 检查输入文本质量 |
## 开发说明
### 添加新的AI服务提供商
`services/ai_service.py` 中:
1. 在 `__init__` 方法中添加配置读取
2. 在 `_determine_ai_provider` 中添加判断逻辑
3. 实现对应的 `_extract_with_xxx` 方法
### 扩展业务类型支持
`services/field_service.py` 中修改 `get_output_fields_by_business_type` 方法,根据不同的 `business_type` 返回对应的字段配置。
## 注意事项
1. **API密钥安全**: 请勿将 `.env` 文件提交到版本控制系统
2. **数据库连接**: 确保数据库服务可访问
3. **网络连接**: AI服务需要访问外部API确保网络畅通
4. **字段配置**: 当前仅支持"初步核实审批表"模板,其他模板需要先在数据库中配置
## 常见问题
**Q: 提示"未配置AI服务"**
A: 检查 `.env` 文件中的 `SILICONFLOW_API_KEY` 是否已正确配置。
**Q: 解析结果为空?**
A: 检查输入文本是否包含足够的信息,可以尝试更详细的输入文本。
**Q: 数据库连接失败?**
A: 检查数据库配置和网络连接,确保数据库服务可访问。
## 许可证
内部项目,仅供内部使用。

113
Swagger文档说明.md Normal file
View File

@ -0,0 +1,113 @@
# Swagger API 文档说明
## 概述
项目已集成 Swagger 文档,可以通过浏览器访问交互式的 API 文档界面。
## 访问方式
启动服务后,在浏览器中访问:
**Swagger UI 文档地址**: `http://localhost:7500/api-docs`
## 功能特性
1. **交互式文档**:可以直接在浏览器中查看所有 API 接口的详细信息
2. **在线测试**:可以在文档页面直接测试 API 接口,无需使用 Postman 或其他工具
3. **参数说明**:详细的请求参数和响应格式说明
4. **错误码说明**:完整的错误码和错误信息说明
## 使用步骤
### 1. 安装依赖
确保已安装 `flasgger` 库:
```bash
pip install -r requirements.txt
```
### 2. 启动服务
```bash
python app.py
```
### 3. 访问文档
在浏览器中打开:`http://localhost:7500/api-docs`
## 接口文档内容
### AI解析接口
- **路径**: `/api/ai/extract`
- **方法**: POST
- **功能**: 从输入数据中提取结构化字段
- **参数说明**:
- `businessType`: 业务类型(必填)
- `inputData`: 输入数据列表(必填)
- `fieldCode`: 字段编码
- `fieldValue`: 字段值(原始文本)
### 字段配置接口
- **路径**: `/api/fields`
- **方法**: GET
- **功能**: 获取字段配置
- **参数说明**:
- `businessType`: 业务类型可选默认INVESTIGATION
## 在 Swagger UI 中测试接口
1. 打开 Swagger 文档页面
2. 找到要测试的接口(如 `/api/ai/extract`
3. 点击 "Try it out" 按钮
4. 填写请求参数
5. 点击 "Execute" 执行请求
6. 查看响应结果
## 示例
### 测试 AI 解析接口
在 Swagger UI 中,找到 `/api/ai/extract` 接口:
1. 点击 "Try it out"
2. 在请求体中填入:
```json
{
"businessType": "INVESTIGATION",
"inputData": [
{
"fieldCode": "clue_info",
"fieldValue": "被举报用户名称是张三年龄30岁某公司总经理"
},
{
"fieldCode": "target_basic_info_clue",
"fieldValue": "张三汉族1980年5月出生山西太原人本科学历2000年参加工作2005年加入中国共产党。"
}
]
}
```
3. 点击 "Execute"
4. 查看响应结果
## 注意事项
1. **服务必须运行**:访问 Swagger 文档前,确保服务已启动
2. **跨域问题**:如果遇到跨域问题,确保已配置 CORS
3. **API 密钥**:测试 AI 解析接口时,确保已配置 `SILICONFLOW_API_KEY` 环境变量
## 相关链接
- **测试页面**: http://localhost:7500/
- **Swagger 文档**: http://localhost:7500/api-docs
- **API 规范 JSON**: http://localhost:7500/apispec.json
## 技术实现
- 使用 `flasgger` 库集成 Swagger
- 通过函数文档字符串docstring自动生成 API 文档
- 支持 OpenAPI 2.0 规范

385
app.py Normal file
View File

@ -0,0 +1,385 @@
"""
智慧监督AI文书写作服务 - 主应用
"""
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from flasgger import Swagger
import os
from datetime import datetime
from dotenv import load_dotenv
from services.ai_service import AIService
from services.field_service import FieldService
from utils.response import success_response, error_response
# 加载环境变量
load_dotenv()
app = Flask(__name__)
CORS(app) # 允许跨域请求
# 配置Swagger
swagger_config = {
"headers": [],
"specs": [
{
"endpoint": "apispec",
"route": "/apispec.json",
"rule_filter": lambda rule: True,
"model_filter": lambda tag: True,
}
],
"static_url_path": "/flasgger_static",
"swagger_ui": True,
"specs_route": "/api-docs"
}
swagger_template = {
"swagger": "2.0",
"info": {
"title": "智慧监督AI文书写作服务 API",
"description": "基于大模型的智能文书生成服务,支持从非结构化文本中提取结构化字段数据",
"version": "1.0.0",
"contact": {
"name": "API支持"
}
},
"basePath": "/",
"schemes": ["http", "https"],
"tags": [
{
"name": "AI解析",
"description": "AI字段提取相关接口"
},
{
"name": "字段配置",
"description": "字段配置查询接口"
}
]
}
swagger = Swagger(app, config=swagger_config, template=swagger_template)
# 初始化服务
ai_service = AIService()
field_service = FieldService()
@app.route('/')
def index():
"""返回测试页面"""
return send_from_directory('static', 'index.html')
@app.route('/api/ai/extract', methods=['POST'])
def extract():
"""
AI字段提取接口
从输入的非结构化文本中提取结构化字段数据
---
tags:
- AI解析
summary: 从输入数据中提取结构化字段
description: 使用AI大模型从输入文本中提取结构化字段支持多种业务类型
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: body
description: 请求参数
required: true
schema:
type: object
required:
- businessType
- inputData
properties:
businessType:
type: string
description: 业务类型
example: INVESTIGATION
inputData:
type: array
description: 输入数据列表
items:
type: object
properties:
fieldCode:
type: string
description: 字段编码
example: clue_info
fieldValue:
type: string
description: 字段值原始文本
example: 被举报用户名称是张三年龄30岁某公司总经理
responses:
200:
description: 解析成功
schema:
type: object
properties:
code:
type: integer
description: 响应码0表示成功
example: 0
data:
type: object
properties:
outData:
type: array
description: 提取的字段列表
items:
type: object
properties:
fieldCode:
type: string
description: 字段编码
example: target_name
fieldValue:
type: string
description: 提取的字段值
example: 张三
msg:
type: string
description: 响应消息
example: ok
isSuccess:
type: boolean
description: 是否成功
example: true
timestamp:
type: string
description: 时间戳
errorMsg:
type: string
description: 错误信息成功时为空
400:
description: 请求参数错误
schema:
type: object
properties:
code:
type: integer
example: 400
errorMsg:
type: string
example: 请求参数不能为空
isSuccess:
type: boolean
example: false
1001:
description: 业务类型不存在
schema:
type: object
properties:
code:
type: integer
example: 1001
errorMsg:
type: string
example: 未找到业务类型 INVESTIGATION 对应的字段配置
isSuccess:
type: boolean
example: false
2001:
description: AI解析超时或发生错误
schema:
type: object
properties:
code:
type: integer
example: 2001
errorMsg:
type: string
example: AI解析超时或发生错误
isSuccess:
type: boolean
example: false
2002:
description: AI解析失败
schema:
type: object
properties:
code:
type: integer
example: 2002
errorMsg:
type: string
example: AI解析失败请检查输入文本质量
isSuccess:
type: boolean
example: false
"""
try:
data = request.get_json()
# 验证请求参数
if not data:
return error_response(400, "请求参数不能为空")
business_type = data.get('businessType')
input_data = data.get('inputData', [])
if not business_type:
return error_response(400, "businessType参数不能为空")
if not input_data or not isinstance(input_data, list):
return error_response(400, "inputData参数必须是非空数组")
# 获取业务类型对应的输出字段
output_fields = field_service.get_output_fields_by_business_type(business_type)
if not output_fields:
return error_response(1001, f"未找到业务类型 {business_type} 对应的字段配置")
# 构建AI提示词
prompt = field_service.build_extract_prompt(input_data, output_fields, business_type)
# 调用AI服务进行解析
ai_result = ai_service.extract_fields(prompt, output_fields)
if not ai_result:
return error_response(2002, "AI解析失败请检查输入文本质量")
# 构建返回数据
out_data = []
for field in output_fields:
field_code = field['field_code']
field_value = ai_result.get(field_code, '')
out_data.append({
'fieldCode': field_code,
'fieldValue': field_value
})
return success_response({'outData': out_data})
except Exception as e:
return error_response(2001, f"AI解析超时或发生错误: {str(e)}")
@app.route('/api/fields', methods=['GET'])
def get_fields():
"""
获取字段配置接口
获取指定业务类型的输入和输出字段配置
---
tags:
- 字段配置
summary: 获取字段配置
description: 获取指定业务类型的输入字段和输出字段配置用于测试页面展示
produces:
- application/json
parameters:
- in: query
name: businessType
type: string
required: false
default: INVESTIGATION
description: 业务类型
example: INVESTIGATION
responses:
200:
description: 获取成功
schema:
type: object
properties:
code:
type: integer
description: 响应码0表示成功
example: 0
data:
type: object
properties:
fields:
type: object
properties:
input_fields:
type: array
description: 输入字段列表
items:
type: object
properties:
id:
type: integer
description: 字段ID
name:
type: string
description: 字段名称
example: 线索信息
field_code:
type: string
description: 字段编码
example: clue_info
field_type:
type: integer
description: 字段类型1=输入字段2=输出字段
example: 1
output_fields:
type: array
description: 输出字段列表
items:
type: object
properties:
id:
type: integer
description: 字段ID
name:
type: string
description: 字段名称
example: 被核查人姓名
field_code:
type: string
description: 字段编码
example: target_name
field_type:
type: integer
description: 字段类型1=输入字段2=输出字段
example: 2
msg:
type: string
description: 响应消息
example: ok
isSuccess:
type: boolean
description: 是否成功
example: true
500:
description: 服务器错误
schema:
type: object
properties:
code:
type: integer
example: 500
errorMsg:
type: string
example: 获取字段配置失败
isSuccess:
type: boolean
example: false
"""
try:
business_type = request.args.get('businessType', 'INVESTIGATION')
fields = field_service.get_fields_by_business_type(business_type)
return success_response({'fields': fields})
except Exception as e:
return error_response(500, f"获取字段配置失败: {str(e)}")
if __name__ == '__main__':
# 确保static目录存在
os.makedirs('static', exist_ok=True)
port = int(os.getenv('PORT', 7500))
debug = os.getenv('DEBUG', 'False').lower() == 'true'
print(f"服务启动在 http://localhost:{port}")
print(f"测试页面: http://localhost:{port}/")
print(f"Swagger API文档: http://localhost:{port}/api-docs")
app.run(host='0.0.0.0', port=port, debug=debug)

137
config/README.md Normal file
View File

@ -0,0 +1,137 @@
# 提示词配置文件说明
## 文件位置
配置文件位于:`config/prompt_config.json`
## 配置文件结构
配置文件采用 JSON 格式,包含以下主要部分:
### 1. prompt_template提示词模板
定义提示词的基本结构和文本标签:
- `intro`: 提示词开头介绍文本
- `input_text_label`: 输入文本部分的标签
- `output_fields_label`: 输出字段部分的标签
- `json_format_label`: JSON格式说明的标签
- `requirements_label`: 要求部分的标签
- `requirements`: 要求列表(数组)
### 2. field_formatting字段格式化
定义输入和输出字段的显示格式:
- `input_field_format`: 输入字段的格式模板,支持 `{field_code}``{field_value}` 占位符
- `output_field_format`: 输出字段的格式模板,支持 `{field_name}``{field_code}` 占位符
### 3. business_type_rules业务类型规则
为不同的业务类型定义特定的规则:
- 每个业务类型可以有自己的配置
- `description`: 业务类型描述
- `additional_requirements`: 该业务类型的额外要求列表
## 修改配置
### 修改提示词文本
直接编辑 `prompt_config.json` 文件中的相应字段即可。例如:
```json
{
"prompt_template": {
"intro": "请从以下输入文本中提取结构化信息。",
"requirements": [
"仔细分析输入文本,准确提取每个字段的值",
"如果某个字段在输入文本中找不到对应信息,该字段值设为空字符串\"\"",
"日期格式统一为YYYYMM198005表示1980年5月"
]
}
}
```
### 修改字段格式
修改 `field_formatting` 部分:
```json
{
"field_formatting": {
"input_field_format": "{field_code}: {field_value}",
"output_field_format": "- {field_name} (字段编码: {field_code})"
}
}
```
### 添加业务类型特定规则
`business_type_rules` 中添加新的业务类型:
```json
{
"business_type_rules": {
"INVESTIGATION": {
"description": "调查核实业务类型的特殊规则",
"additional_requirements": [
"特别注意提取被核查人的基本信息",
"确保日期格式正确"
]
},
"NEW_BUSINESS_TYPE": {
"description": "新业务类型的规则",
"additional_requirements": [
"新业务类型的特殊要求"
]
}
}
}
```
## 配置生效
修改配置文件后,需要重启服务才能生效。
## 注意事项
1. **JSON 格式**:确保 JSON 格式正确,可以使用在线 JSON 验证工具检查
2. **编码**:配置文件使用 UTF-8 编码
3. **转义字符**:在 JSON 字符串中使用引号时,需要使用 `\"` 转义
4. **默认配置**:如果配置文件不存在或格式错误,系统会使用代码中的默认配置
## 示例
完整的配置文件示例:
```json
{
"prompt_template": {
"intro": "请从以下输入文本中提取结构化信息。",
"input_text_label": "输入文本:",
"output_fields_label": "需要提取的字段:",
"json_format_label": "请严格按照以下JSON格式返回结果只返回JSON不要包含其他文字说明",
"requirements_label": "要求:",
"requirements": [
"仔细分析输入文本,准确提取每个字段的值",
"如果某个字段在输入文本中找不到对应信息,该字段值设为空字符串\"\"",
"日期格式统一为YYYYMM198005表示1980年5月",
"性别统一为\"男\"或\"女\"",
"政治面貌使用标准表述(如:中共党员、群众等)",
"只返回JSON对象不要包含markdown代码块标记"
]
},
"field_formatting": {
"input_field_format": "{field_code}: {field_value}",
"output_field_format": "- {field_name} (字段编码: {field_code})"
},
"business_type_rules": {
"INVESTIGATION": {
"description": "调查核实业务类型的特殊规则",
"additional_requirements": []
}
}
}
```

28
config/prompt_config.json Normal file
View File

@ -0,0 +1,28 @@
{
"prompt_template": {
"intro": "请从以下输入文本中提取结构化信息。",
"input_text_label": "输入文本:",
"output_fields_label": "需要提取的字段:",
"json_format_label": "请严格按照以下JSON格式返回结果只返回JSON不要包含其他文字说明",
"requirements_label": "要求:",
"requirements": [
"仔细分析输入文本,准确提取每个字段的值",
"如果某个字段在输入文本中找不到对应信息,该字段值设为空字符串\"\"",
"日期格式统一为YYYYMM198005表示1980年5月",
"性别统一为\"男\"或\"女\"",
"政治面貌使用标准表述(如:中共党员、群众等)",
"只返回JSON对象不要包含markdown代码块标记"
]
},
"field_formatting": {
"input_field_format": "{field_code}: {field_value}",
"output_field_format": "- {field_name} (字段编码: {field_code})"
},
"business_type_rules": {
"INVESTIGATION": {
"description": "调查核实业务类型的特殊规则",
"additional_requirements": []
}
}
}

View File

@ -0,0 +1,298 @@
"""
初步核实审批表字段数据初始化脚本
根据Excel数据字段汇总表和Word模板分析创建相关字段配置
"""
import pymysql
from datetime import datetime
import uuid
# 数据库连接配置
DB_CONFIG = {
'host': '152.136.177.240',
'port': 5012,
'user': 'finyx',
'password': '6QsGK6MpePZDE57Z',
'database': 'finyx',
'charset': 'utf8mb4'
}
# 固定值
TENANT_ID = 615873064429507639 # 从现有数据中获取
CREATED_BY = 655162080928945152 # 从现有数据中获取
CURRENT_TIME = datetime.now()
# 根据Excel数据设计的字段配置
# 字段类型1-输入字段2-输出字段
FIELDS = [
{
'name': '被核查人姓名',
'field_code': 'target_name',
'field_type': 2, # 输出字段
'description': '被核查人姓名'
},
{
'name': '被核查人员单位及职务',
'field_code': 'target_organization_and_position',
'field_type': 2, # 输出字段
'description': '被核查人员单位及职务(包括兼职)'
},
{
'name': '被核查人员性别',
'field_code': 'target_gender',
'field_type': 2, # 输出字段
'description': '被核查人员性别(男/女,不用男性和女性)'
},
{
'name': '被核查人员出生年月',
'field_code': 'target_date_of_birth',
'field_type': 2, # 输出字段
'description': '被核查人员出生年月YYYYMM格式不需要日'
},
{
'name': '被核查人员政治面貌',
'field_code': 'target_political_status',
'field_type': 2, # 输出字段
'description': '被核查人员政治面貌(中共党员、群众等)'
},
{
'name': '被核查人员职级',
'field_code': 'target_professional_rank',
'field_type': 2, # 输出字段
'description': '被核查人员职级(如:正处级)'
},
{
'name': '线索来源',
'field_code': 'clue_source',
'field_type': 2, # 输出字段
'description': '线索来源'
},
{
'name': '主要问题线索',
'field_code': 'target_issue_description',
'field_type': 2, # 输出字段
'description': '主要问题线索描述'
},
{
'name': '初步核实审批表承办部门意见',
'field_code': 'department_opinion',
'field_type': 2, # 输出字段
'description': '初步核实审批表承办部门意见'
},
{
'name': '初步核实审批表填表人',
'field_code': 'filler_name',
'field_type': 2, # 输出字段
'description': '初步核实审批表填表人'
},
{
'name': '线索信息',
'field_code': 'clue_info',
'field_type': 1, # 输入字段
'description': '线索信息用于AI解析'
},
{
'name': '被核查人员工作基本情况线索',
'field_code': 'target_basic_info_clue',
'field_type': 1, # 输入字段
'description': '被核查人员工作基本情况线索用于AI解析'
}
]
# 文件配置
FILE_CONFIG = {
'name': '初步核实审批表',
'template_code': 'PRELIMINARY_VERIFICATION_APPROVAL',
'file_path': '/templates/初步核实审批表模板.docx', # MinIO相对路径
'business_type': 'INVESTIGATION', # 调查核实
'parent_id': None # 顶级分类,可以根据实际情况设置
}
def generate_id():
"""生成ID使用时间戳+随机数的方式,模拟雪花算法)"""
import time
import random
timestamp = int(time.time() * 1000)
random_part = random.randint(100000, 999999)
return timestamp * 1000 + random_part
def init_fields(conn):
"""初始化字段数据"""
cursor = conn.cursor()
field_ids = {}
print("="*60)
print("开始初始化字段数据...")
print("="*60)
for field in FIELDS:
# 检查字段是否已存在
check_sql = """
SELECT id FROM f_polic_field
WHERE tenant_id = %s AND filed_code = %s
"""
cursor.execute(check_sql, (TENANT_ID, field['field_code']))
existing = cursor.fetchone()
if existing:
field_id = existing[0]
print(f"字段 '{field['name']}' (code: {field['field_code']}) 已存在ID: {field_id}")
else:
field_id = generate_id()
insert_sql = """
INSERT INTO f_polic_field
(id, tenant_id, name, filed_code, field_type, created_time, created_by, updated_time, updated_by, state)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(insert_sql, (
field_id,
TENANT_ID,
field['name'],
field['field_code'],
field['field_type'],
CURRENT_TIME,
CREATED_BY,
CURRENT_TIME,
CREATED_BY,
0 # state: 0表示未启用1表示启用
))
print(f"✓ 创建字段: {field['name']} (code: {field['field_code']}), ID: {field_id}")
field_ids[field['field_code']] = field_id
conn.commit()
return field_ids
def init_file_config(conn):
"""初始化文件配置"""
cursor = conn.cursor()
print("\n" + "="*60)
print("开始初始化文件配置...")
print("="*60)
# 检查文件配置是否已存在
check_sql = """
SELECT id FROM f_polic_file_config
WHERE tenant_id = %s AND name = %s
"""
cursor.execute(check_sql, (TENANT_ID, FILE_CONFIG['name']))
existing = cursor.fetchone()
if existing:
file_config_id = existing[0]
print(f"文件配置 '{FILE_CONFIG['name']}' 已存在ID: {file_config_id}")
else:
file_config_id = generate_id()
insert_sql = """
INSERT INTO f_polic_file_config
(id, tenant_id, parent_id, name, input_data, file_path, created_time, created_by, updated_time, updated_by, state)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
# input_data字段存储模板编码和业务类型JSON格式
import json
input_data = json.dumps({
'template_code': FILE_CONFIG['template_code'],
'business_type': FILE_CONFIG['business_type']
}, ensure_ascii=False)
cursor.execute(insert_sql, (
file_config_id,
TENANT_ID,
FILE_CONFIG['parent_id'],
FILE_CONFIG['name'],
input_data,
FILE_CONFIG['file_path'],
CURRENT_TIME,
CREATED_BY,
CURRENT_TIME,
CREATED_BY,
1 # state: 1表示启用
))
print(f"✓ 创建文件配置: {FILE_CONFIG['name']}, ID: {file_config_id}")
conn.commit()
return file_config_id
def init_file_field_relations(conn, file_config_id, field_ids):
"""初始化文件和字段的关联关系"""
cursor = conn.cursor()
print("\n" + "="*60)
print("开始建立文件和字段的关联关系...")
print("="*60)
# 只关联输出字段field_type=2
output_fields = {k: v for k, v in field_ids.items()
if any(f['field_code'] == k and f['field_type'] == 2 for f in FIELDS)}
for field_code, field_id in output_fields.items():
# 检查关联关系是否已存在
check_sql = """
SELECT id FROM f_polic_file_field
WHERE tenant_id = %s AND filed_id = %s AND file_id = %s
"""
cursor.execute(check_sql, (TENANT_ID, field_id, file_config_id))
existing = cursor.fetchone()
if existing:
print(f"关联关系已存在: 文件ID {file_config_id} <-> 字段ID {field_id} ({field_code})")
else:
insert_sql = """
INSERT INTO f_polic_file_field
(tenant_id, filed_id, file_id, created_time, created_by, updated_time, updated_by, state)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(insert_sql, (
TENANT_ID,
field_id,
file_config_id,
CURRENT_TIME,
CREATED_BY,
CURRENT_TIME,
CREATED_BY,
0 # state: 0表示未启用1表示启用
))
field_name = next(f['name'] for f in FIELDS if f['field_code'] == field_code)
print(f"✓ 建立关联: {field_name} ({field_code})")
conn.commit()
def main():
"""主函数"""
try:
# 连接数据库
conn = pymysql.connect(**DB_CONFIG)
print("数据库连接成功!\n")
# 初始化字段
field_ids = init_fields(conn)
# 初始化文件配置
file_config_id = init_file_config(conn)
# 建立关联关系
init_file_field_relations(conn, file_config_id, field_ids)
print("\n" + "="*60)
print("初始化完成!")
print("="*60)
print(f"\n文件配置ID: {file_config_id}")
print(f"创建的字段数量: {len(field_ids)}")
print(f"建立的关联关系数量: {len([f for f in FIELDS if f['field_type'] == 2])}")
conn.close()
except Exception as e:
print(f"\n错误: {e}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
main()

7
requirements.txt Normal file
View File

@ -0,0 +1,7 @@
flask==3.0.0
flask-cors==4.0.0
pymysql==1.1.2
python-dotenv==1.0.0
requests==2.31.0
flasgger==0.9.7.1

2
services/__init__.py Normal file
View File

@ -0,0 +1,2 @@
# Services package

Binary file not shown.

Binary file not shown.

Binary file not shown.

158
services/ai_service.py Normal file
View File

@ -0,0 +1,158 @@
"""
AI服务 - 封装大模型调用
支持硅基流动和华为大模型预留
"""
import os
import requests
import json
from typing import Dict, List, Optional
class AIService:
"""AI服务类"""
def __init__(self):
self.siliconflow_api_key = os.getenv('SILICONFLOW_API_KEY')
self.siliconflow_model = os.getenv('SILICONFLOW_MODEL', 'deepseek-ai/DeepSeek-V3.2-Exp')
self.siliconflow_url = "https://api.siliconflow.cn/v1/chat/completions"
# 华为大模型配置(预留)
self.huawei_api_endpoint = os.getenv('HUAWEI_API_ENDPOINT')
self.huawei_api_key = os.getenv('HUAWEI_API_KEY')
# 确定使用的AI服务
self.ai_provider = self._determine_ai_provider()
def _determine_ai_provider(self) -> str:
"""确定使用的AI服务提供商"""
if self.siliconflow_api_key:
return 'siliconflow'
elif self.huawei_api_endpoint and self.huawei_api_key:
return 'huawei'
else:
return 'none'
def extract_fields(self, prompt: str, output_fields: List[Dict]) -> Optional[Dict]:
"""
从提示词中提取结构化字段
Args:
prompt: AI提示词
output_fields: 输出字段列表
Returns:
提取的字段字典格式: {field_code: field_value}
"""
if self.ai_provider == 'none':
raise Exception("未配置AI服务请设置SILICONFLOW_API_KEY或华为大模型配置")
if self.ai_provider == 'siliconflow':
return self._extract_with_siliconflow(prompt, output_fields)
elif self.ai_provider == 'huawei':
return self._extract_with_huawei(prompt, output_fields)
else:
raise Exception(f"未知的AI服务提供商: {self.ai_provider}")
def _extract_with_siliconflow(self, prompt: str, output_fields: List[Dict]) -> Optional[Dict]:
"""使用硅基流动API提取字段"""
try:
payload = {
"model": self.siliconflow_model,
"messages": [
{
"role": "system",
"content": "你是一个专业的数据提取助手能够从文本中准确提取结构化信息。请严格按照JSON格式返回结果。"
},
{
"role": "user",
"content": prompt
}
],
"temperature": 0.3,
"max_tokens": 2000
}
headers = {
"Authorization": f"Bearer {self.siliconflow_api_key}",
"Content-Type": "application/json"
}
response = requests.post(
self.siliconflow_url,
json=payload,
headers=headers,
timeout=30
)
if response.status_code != 200:
raise Exception(f"API调用失败: {response.status_code} - {response.text}")
result = response.json()
# 提取AI返回的内容
if 'choices' in result and len(result['choices']) > 0:
content = result['choices'][0]['message']['content']
# 尝试解析JSON
try:
# 如果返回的是代码块提取JSON部分
if '```json' in content:
json_start = content.find('```json') + 7
json_end = content.find('```', json_start)
content = content[json_start:json_end].strip()
elif '```' in content:
json_start = content.find('```') + 3
json_end = content.find('```', json_start)
content = content[json_start:json_end].strip()
extracted_data = json.loads(content)
return extracted_data
except json.JSONDecodeError:
# 如果不是JSON尝试从文本中提取
return self._parse_text_response(content, output_fields)
else:
raise Exception("API返回格式异常")
except requests.exceptions.Timeout:
raise Exception("AI服务调用超时")
except Exception as e:
raise Exception(f"AI服务调用失败: {str(e)}")
def _extract_with_huawei(self, prompt: str, output_fields: List[Dict]) -> Optional[Dict]:
"""使用华为大模型API提取字段预留实现"""
# TODO: 实现华为大模型接口调用
raise Exception("华为大模型接口暂未实现请使用硅基流动API")
def _parse_text_response(self, text: str, output_fields: List[Dict]) -> Dict:
"""
从文本响应中解析字段值备用方案
"""
result = {}
for field in output_fields:
field_code = field['field_code']
field_name = field['name']
# 尝试在文本中查找字段值
# 这里使用简单的关键词匹配,实际可以更复杂
if field_name in text:
# 提取字段值(简单实现)
start_idx = text.find(field_name)
if start_idx != -1:
# 查找冒号后的内容
colon_idx = text.find(':', start_idx)
if colon_idx != -1:
value_start = colon_idx + 1
value_end = text.find('\n', value_start)
if value_end == -1:
value_end = len(text)
value = text[value_start:value_end].strip()
result[field_code] = value
else:
result[field_code] = ''
else:
result[field_code] = ''
else:
result[field_code] = ''
return result

263
services/field_service.py Normal file
View File

@ -0,0 +1,263 @@
"""
字段服务 - 从数据库获取字段配置
"""
import pymysql
import os
import json
from typing import List, Dict, Optional
from pathlib import Path
class FieldService:
"""字段服务类"""
def __init__(self):
self.db_config = {
'host': os.getenv('DB_HOST', '152.136.177.240'),
'port': int(os.getenv('DB_PORT', 5012)),
'user': os.getenv('DB_USER', 'finyx'),
'password': os.getenv('DB_PASSWORD', '6QsGK6MpePZDE57Z'),
'database': os.getenv('DB_NAME', 'finyx'),
'charset': 'utf8mb4'
}
self.tenant_id = 615873064429507639
# 加载提示词配置文件
self.prompt_config = self._load_prompt_config()
def _load_prompt_config(self) -> Dict:
"""
加载提示词配置文件
Returns:
配置字典
"""
# 获取项目根目录
current_dir = Path(__file__).parent
project_root = current_dir.parent
config_path = project_root / 'config' / 'prompt_config.json'
try:
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
return config
except FileNotFoundError:
# 如果配置文件不存在,使用默认配置
print(f"警告: 配置文件 {config_path} 不存在,使用默认配置")
return self._get_default_config()
except json.JSONDecodeError as e:
print(f"错误: 配置文件 {config_path} JSON格式错误: {e}")
return self._get_default_config()
def _get_default_config(self) -> Dict:
"""获取默认配置(作为后备方案)"""
return {
"prompt_template": {
"intro": "请从以下输入文本中提取结构化信息。",
"input_text_label": "输入文本:",
"output_fields_label": "需要提取的字段:",
"json_format_label": "请严格按照以下JSON格式返回结果只返回JSON不要包含其他文字说明",
"requirements_label": "要求:",
"requirements": [
"仔细分析输入文本,准确提取每个字段的值",
"如果某个字段在输入文本中找不到对应信息,该字段值设为空字符串\"\"",
"日期格式统一为YYYYMM198005表示1980年5月",
"性别统一为\"\"\"\"",
"政治面貌使用标准表述(如:中共党员、群众等)",
"只返回JSON对象不要包含markdown代码块标记"
]
},
"field_formatting": {
"input_field_format": "{field_code}: {field_value}",
"output_field_format": "- {field_name} (字段编码: {field_code})"
},
"business_type_rules": {
"INVESTIGATION": {
"description": "调查核实业务类型的特殊规则",
"additional_requirements": []
}
}
}
def get_connection(self):
"""获取数据库连接"""
return pymysql.connect(**self.db_config)
def get_output_fields_by_business_type(self, business_type: str) -> List[Dict]:
"""
根据业务类型获取输出字段列表
Args:
business_type: 业务类型 'INVESTIGATION'
Returns:
字段列表每个字段包含: id, name, field_code, field_type
"""
conn = self.get_connection()
cursor = conn.cursor(pymysql.cursors.DictCursor)
try:
# 查询"初步核实审批表"相关的输出字段field_type=2
# 目前只支持初步核实审批表后续可以根据business_type扩展
sql = """
SELECT f.id, f.name, f.filed_code as field_code, f.field_type
FROM f_polic_field f
INNER JOIN f_polic_file_field ff ON f.id = ff.filed_id
INNER JOIN f_polic_file_config fc ON ff.file_id = fc.id
WHERE f.tenant_id = %s
AND f.field_type = 2
AND fc.name = '初步核实审批表'
ORDER BY f.id
"""
cursor.execute(sql, (self.tenant_id,))
fields = cursor.fetchall()
# 转换为字典列表
result = []
for field in fields:
result.append({
'id': field['id'],
'name': field['name'],
'field_code': field['field_code'],
'field_type': field['field_type']
})
return result
finally:
cursor.close()
conn.close()
def get_fields_by_business_type(self, business_type: str) -> Dict:
"""
获取业务类型的所有字段包括输入和输出字段
用于测试页面展示
"""
conn = self.get_connection()
cursor = conn.cursor(pymysql.cursors.DictCursor)
try:
# 获取输入字段field_type=1
sql_input = """
SELECT f.id, f.name, f.filed_code as field_code, f.field_type
FROM f_polic_field f
WHERE f.tenant_id = %s
AND f.field_type = 1
AND (f.filed_code = 'clue_info' OR f.filed_code = 'target_basic_info_clue')
ORDER BY f.id
"""
cursor.execute(sql_input, (self.tenant_id,))
input_fields = cursor.fetchall()
# 获取输出字段field_type=2
sql_output = """
SELECT f.id, f.name, f.filed_code as field_code, f.field_type
FROM f_polic_field f
INNER JOIN f_polic_file_field ff ON f.id = ff.filed_id
INNER JOIN f_polic_file_config fc ON ff.file_id = fc.id
WHERE f.tenant_id = %s
AND f.field_type = 2
AND fc.name = '初步核实审批表'
ORDER BY f.id
"""
cursor.execute(sql_output, (self.tenant_id,))
output_fields = cursor.fetchall()
return {
'input_fields': [
{
'id': f['id'],
'name': f['name'],
'field_code': f['field_code'],
'field_type': f['field_type']
}
for f in input_fields
],
'output_fields': [
{
'id': f['id'],
'name': f['name'],
'field_code': f['field_code'],
'field_type': f['field_type']
}
for f in output_fields
]
}
finally:
cursor.close()
conn.close()
def build_extract_prompt(self, input_data: List[Dict], output_fields: List[Dict], business_type: str = 'INVESTIGATION') -> str:
"""
构建AI提取提示词
Args:
input_data: 输入数据列表格式: [{'fieldCode': 'xxx', 'fieldValue': 'xxx'}]
output_fields: 输出字段列表
business_type: 业务类型用于获取特定规则
Returns:
构建好的提示词
"""
# 获取配置
template = self.prompt_config.get('prompt_template', {})
formatting = self.prompt_config.get('field_formatting', {})
business_rules = self.prompt_config.get('business_type_rules', {}).get(business_type, {})
# 构建输入文本
input_field_format = formatting.get('input_field_format', '{field_code}: {field_value}')
input_text = ""
for item in input_data:
field_code = item.get('fieldCode', '')
field_value = item.get('fieldValue', '')
input_text += input_field_format.format(
field_code=field_code,
field_value=field_value
) + "\n"
# 构建输出字段说明
output_field_format = formatting.get('output_field_format', '- {field_name} (字段编码: {field_code})')
output_fields_desc = ""
for field in output_fields:
field_name = field['name']
field_code = field['field_code']
output_fields_desc += output_field_format.format(
field_name=field_name,
field_code=field_code
) + "\n"
# 构建JSON格式示例
json_example = {}
for field in output_fields:
json_example[field['field_code']] = ""
# 获取要求列表
requirements = template.get('requirements', [])
# 添加业务类型特定的要求
additional_requirements = business_rules.get('additional_requirements', [])
all_requirements = requirements + additional_requirements
# 构建要求文本
requirements_text = ""
for i, req in enumerate(all_requirements, 1):
requirements_text += f"{i}. {req}\n"
# 构建完整提示词
prompt = f"""{template.get('intro', '请从以下输入文本中提取结构化信息。')}
{template.get('input_text_label', '输入文本:')}
{input_text.strip()}
{template.get('output_fields_label', '需要提取的字段:')}
{output_fields_desc.strip()}
{template.get('json_format_label', '请严格按照以下JSON格式返回结果只返回JSON不要包含其他文字说明')}
{json.dumps(json_example, ensure_ascii=False, indent=2)}
{template.get('requirements_label', '要求:')}
{requirements_text.strip()}
"""
return prompt

25
setup_env.bat Normal file
View File

@ -0,0 +1,25 @@
@echo off
REM 创建虚拟环境并安装依赖Windows
echo 创建Python虚拟环境...
python -m venv venv
echo 激活虚拟环境...
call venv\Scripts\activate.bat
echo 安装依赖...
pip install -r requirements.txt
echo 复制环境变量配置文件...
if not exist .env (
copy .env.example .env
echo 请编辑 .env 文件填入你的API密钥
)
echo 完成!
echo 使用以下命令启动服务:
echo venv\Scripts\activate
echo python app.py
pause

23
setup_env.sh Normal file
View File

@ -0,0 +1,23 @@
#!/bin/bash
# 创建虚拟环境并安装依赖Linux/Mac
echo "创建Python虚拟环境..."
python3 -m venv venv
echo "激活虚拟环境..."
source venv/bin/activate
echo "安装依赖..."
pip install -r requirements.txt
echo "复制环境变量配置文件..."
if [ ! -f .env ]; then
cp .env.example .env
echo "请编辑 .env 文件填入你的API密钥"
fi
echo "完成!"
echo "使用以下命令启动服务:"
echo " source venv/bin/activate"
echo " python app.py"

440
static/index.html Normal file
View File

@ -0,0 +1,440 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>智慧监督AI文书写作 - 解析接口测试</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Microsoft YaHei', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
text-align: center;
}
.header h1 {
font-size: 28px;
margin-bottom: 10px;
}
.header p {
opacity: 0.9;
font-size: 14px;
}
.content {
padding: 30px;
}
.section {
margin-bottom: 30px;
}
.section-title {
font-size: 18px;
font-weight: 600;
color: #333;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 2px solid #667eea;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: 500;
}
.form-group input,
.form-group select,
.form-group textarea {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 6px;
font-size: 14px;
transition: border-color 0.3s;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
outline: none;
border-color: #667eea;
}
.form-group textarea {
min-height: 100px;
resize: vertical;
font-family: inherit;
}
.input-fields {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 15px;
margin-bottom: 15px;
}
.input-fields input {
margin-bottom: 0;
}
.btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 30px;
border-radius: 6px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
.btn:active {
transform: translateY(0);
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.result-box {
background: #f8f9fa;
border: 2px solid #e0e0e0;
border-radius: 6px;
padding: 20px;
margin-top: 20px;
max-height: 500px;
overflow-y: auto;
}
.result-box pre {
margin: 0;
font-family: 'Courier New', monospace;
font-size: 13px;
line-height: 1.6;
white-space: pre-wrap;
word-wrap: break-word;
}
.result-item {
background: white;
padding: 15px;
margin-bottom: 10px;
border-radius: 6px;
border-left: 4px solid #667eea;
}
.result-item strong {
color: #667eea;
display: inline-block;
min-width: 150px;
}
.loading {
display: none;
text-align: center;
padding: 20px;
}
.loading.active {
display: block;
}
.spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid #667eea;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 0 auto 10px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.error {
background: #fee;
border-left-color: #f44336;
color: #c62828;
}
.success {
background: #e8f5e9;
border-left-color: #4caf50;
}
.add-field-btn {
background: #4caf50;
padding: 8px 15px;
font-size: 14px;
margin-top: 10px;
}
.remove-field-btn {
background: #f44336;
padding: 6px 12px;
font-size: 12px;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>智慧监督AI文书写作</h1>
<p>解析接口测试工具 - 初步核实审批表</p>
</div>
<div class="content">
<!-- 输入区域 -->
<div class="section">
<div class="section-title">输入数据</div>
<div class="form-group">
<label>业务类型</label>
<select id="businessType">
<option value="INVESTIGATION">INVESTIGATION (调查核实)</option>
<option value="COMPLAINT">COMPLAINT (投诉举报)</option>
</select>
</div>
<div class="form-group">
<label>输入字段数据</label>
<div id="inputFieldsContainer">
<!-- 动态生成的输入字段 -->
</div>
<button class="btn add-field-btn" onclick="addInputField()">+ 添加输入字段</button>
</div>
</div>
<!-- 操作按钮 -->
<div class="section">
<button class="btn" onclick="extractData()" id="extractBtn">开始解析</button>
</div>
<!-- 加载提示 -->
<div class="loading" id="loading">
<div class="spinner"></div>
<p>AI正在解析中请稍候...</p>
</div>
<!-- 结果展示 -->
<div class="section" id="resultSection" style="display: none;">
<div class="section-title">解析结果</div>
<div class="result-box" id="resultBox"></div>
</div>
</div>
</div>
<script>
let inputFields = [];
let outputFields = [];
// 页面加载时初始化
window.onload = function() {
loadFields();
initDefaultInputFields();
};
// 加载字段配置
async function loadFields() {
try {
const response = await fetch('/api/fields?businessType=INVESTIGATION');
const result = await response.json();
if (result.isSuccess && result.data) {
outputFields = result.data.output_fields || [];
// 可以在这里使用outputFields来显示字段信息
}
} catch (error) {
console.error('加载字段配置失败:', error);
}
}
// 初始化默认输入字段
function initDefaultInputFields() {
addInputField('clue_info', '线索信息', '被举报用户名称是张三年龄30岁某公司总经理');
addInputField('target_basic_info_clue', '被核查人员工作基本情况线索', '张三汉族1980年5月出生山西太原人本科学历2000年参加工作2005年加入中国共产党。');
}
// 添加输入字段
function addInputField(fieldCode = '', fieldName = '', fieldValue = '') {
const container = document.getElementById('inputFieldsContainer');
const fieldDiv = document.createElement('div');
fieldDiv.className = 'input-fields';
fieldDiv.innerHTML = `
<input type="text" placeholder="字段编码 (如: clue_info)" value="${fieldCode}" class="field-code">
<div style="display: flex; gap: 10px;">
<input type="text" placeholder="字段值" value="${fieldValue}" class="field-value" style="flex: 1;">
<button class="btn remove-field-btn" onclick="removeInputField(this)">删除</button>
</div>
`;
container.appendChild(fieldDiv);
}
// 删除输入字段
function removeInputField(btn) {
btn.closest('.input-fields').remove();
}
// 提取数据
async function extractData() {
const extractBtn = document.getElementById('extractBtn');
const loading = document.getElementById('loading');
const resultSection = document.getElementById('resultSection');
const resultBox = document.getElementById('resultBox');
// 收集输入数据
const inputFields = [];
const fieldContainers = document.querySelectorAll('#inputFieldsContainer .input-fields');
fieldContainers.forEach(container => {
const fieldCode = container.querySelector('.field-code').value.trim();
const fieldValue = container.querySelector('.field-value').value.trim();
if (fieldCode && fieldValue) {
inputFields.push({
fieldCode: fieldCode,
fieldValue: fieldValue
});
}
});
if (inputFields.length === 0) {
alert('请至少添加一个输入字段');
return;
}
const businessType = document.getElementById('businessType').value;
// 构建请求数据
const requestData = {
businessType: businessType,
inputData: inputFields
};
// 显示加载状态
extractBtn.disabled = true;
loading.classList.add('active');
resultSection.style.display = 'none';
try {
const response = await fetch('/api/ai/extract', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestData)
});
const result = await response.json();
// 显示结果
displayResult(result, requestData);
} catch (error) {
displayError(error.message);
} finally {
extractBtn.disabled = false;
loading.classList.remove('active');
}
}
// 显示结果
function displayResult(result, requestData) {
const resultSection = document.getElementById('resultSection');
const resultBox = document.getElementById('resultBox');
resultSection.style.display = 'block';
let html = '';
// 显示请求数据
html += '<div class="result-item"><strong>请求数据:</strong></div>';
html += '<div class="result-item"><pre>' + JSON.stringify(requestData, null, 2) + '</pre></div>';
// 显示响应结果
if (result.isSuccess) {
html += '<div class="result-item success"><strong>解析成功!</strong></div>';
if (result.data && result.data.outData) {
html += '<div class="result-item"><strong>提取的字段:</strong></div>';
result.data.outData.forEach(item => {
html += `<div class="result-item">
<strong>${item.fieldCode}:</strong> ${item.fieldValue || '(空)'}
</div>`;
});
}
} else {
html += `<div class="result-item error">
<strong>解析失败!</strong><br>
错误码: ${result.code}<br>
错误信息: ${result.errorMsg}
</div>`;
}
// 显示完整响应
html += '<div class="result-item"><strong>完整响应:</strong></div>';
html += '<div class="result-item"><pre>' + JSON.stringify(result, null, 2) + '</pre></div>';
resultBox.innerHTML = html;
// 滚动到结果区域
resultSection.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
// 显示错误
function displayError(errorMsg) {
const resultSection = document.getElementById('resultSection');
const resultBox = document.getElementById('resultBox');
resultSection.style.display = 'block';
resultBox.innerHTML = `<div class="result-item error">
<strong>请求失败!</strong><br>
${errorMsg}
</div>`;
}
</script>
</body>
</html>

130
test_service.py Normal file
View File

@ -0,0 +1,130 @@
"""
测试服务是否能正常启动和运行
"""
import sys
import os
# 添加当前目录到路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_imports():
"""测试所有模块是否能正常导入"""
print("="*60)
print("测试模块导入...")
print("="*60)
try:
from flask import Flask
print("✓ Flask 导入成功")
except ImportError as e:
print(f"✗ Flask 导入失败: {e}")
return False
try:
from services.ai_service import AIService
print("✓ AIService 导入成功")
except ImportError as e:
print(f"✗ AIService 导入失败: {e}")
return False
try:
from services.field_service import FieldService
print("✓ FieldService 导入成功")
except ImportError as e:
print(f"✗ FieldService 导入失败: {e}")
return False
try:
from utils.response import success_response, error_response
print("✓ response 工具导入成功")
except ImportError as e:
print(f"✗ response 工具导入失败: {e}")
return False
return True
def test_field_service():
"""测试字段服务"""
print("\n" + "="*60)
print("测试字段服务...")
print("="*60)
try:
from services.field_service import FieldService
field_service = FieldService()
# 测试获取字段
fields = field_service.get_output_fields_by_business_type('INVESTIGATION')
print(f"✓ 成功获取 {len(fields)} 个输出字段")
if fields:
print(f" 示例字段: {fields[0].get('name', 'N/A')} ({fields[0].get('field_code', 'N/A')})")
return True
except Exception as e:
print(f"✗ 字段服务测试失败: {e}")
import traceback
traceback.print_exc()
return False
def test_ai_service():
"""测试AI服务"""
print("\n" + "="*60)
print("测试AI服务...")
print("="*60)
try:
from services.ai_service import AIService
ai_service = AIService()
print(f"✓ AI服务初始化成功")
print(f" AI提供商: {ai_service.ai_provider}")
if ai_service.ai_provider == 'none':
print(" ⚠ 警告: 未配置AI服务请设置SILICONFLOW_API_KEY")
elif ai_service.ai_provider == 'siliconflow':
print(" ✓ 已配置硅基流动API")
elif ai_service.ai_provider == 'huawei':
print(" ✓ 已配置华为大模型API")
return True
except Exception as e:
print(f"✗ AI服务测试失败: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""主测试函数"""
print("\n" + "="*60)
print("智慧监督AI文书写作服务 - 启动测试")
print("="*60)
# 测试导入
if not test_imports():
print("\n✗ 模块导入测试失败,请检查依赖是否安装")
return False
# 测试字段服务
if not test_field_service():
print("\n✗ 字段服务测试失败")
return False
# 测试AI服务
if not test_ai_service():
print("\n✗ AI服务测试失败")
return False
print("\n" + "="*60)
print("✓ 所有测试通过!")
print("="*60)
print("\n可以使用以下命令启动服务:")
print(" python app.py")
print("\n然后在浏览器访问: http://localhost:5000/")
return True
if __name__ == '__main__':
success = main()
sys.exit(0 if success else 1)

192
upload_template_to_minio.py Normal file
View File

@ -0,0 +1,192 @@
"""
将初步核实审批表模板上传到MinIO并更新数据库
"""
import pymysql
from minio import Minio
from minio.error import S3Error
from datetime import datetime
import os
from urllib.parse import urlparse
# MinIO连接配置
MINIO_CONFIG = {
'endpoint': 'minio.datacubeworld.com:9000',
'access_key': 'JOLXFXny3avFSzB0uRA5',
'secret_key': 'G1BR8jStNfovkfH5ou39EmPl34E4l7dGrnd3Cz0I',
'secure': True # 使用HTTPS
}
# 数据库连接配置
DB_CONFIG = {
'host': '152.136.177.240',
'port': 5012,
'user': 'finyx',
'password': '6QsGK6MpePZDE57Z',
'database': 'finyx',
'charset': 'utf8mb4'
}
# 固定值
TENANT_ID = 615873064429507639
UPDATED_BY = 655162080928945152
CURRENT_TIME = datetime.now()
# 文件配置
TEMPLATE_FILE = '模板/初步核实审批表模板.docx'
BUCKET_NAME = 'finyx' # 存储桶名称
TENANT_ID = '615873064429507639'
# MinIO中的对象名称相对路径按照现有目录结构组织
from datetime import datetime
now = datetime.now()
OBJECT_NAME = f'{TENANT_ID}/TEMPLATE/{now.year}/{now.month:02d}/初步核实审批表模板.docx'
def upload_to_minio():
"""上传文件到MinIO"""
print("="*60)
print("开始上传文件到MinIO...")
print("="*60)
try:
# 创建MinIO客户端
client = Minio(
MINIO_CONFIG['endpoint'],
access_key=MINIO_CONFIG['access_key'],
secret_key=MINIO_CONFIG['secret_key'],
secure=MINIO_CONFIG['secure']
)
# 检查存储桶是否存在
found = client.bucket_exists(BUCKET_NAME)
if not found:
raise Exception(f"存储桶 '{BUCKET_NAME}' 不存在,请先创建")
else:
print(f"✓ 存储桶 '{BUCKET_NAME}' 已存在")
# 检查文件是否存在
if not os.path.exists(TEMPLATE_FILE):
raise FileNotFoundError(f"模板文件不存在: {TEMPLATE_FILE}")
file_size = os.path.getsize(TEMPLATE_FILE)
print(f"\n文件信息:")
print(f" 本地路径: {TEMPLATE_FILE}")
print(f" 文件大小: {file_size} 字节")
print(f" 存储桶: {BUCKET_NAME}")
print(f" 对象名称: {OBJECT_NAME}")
# 上传文件
print(f"\n正在上传文件...")
client.fput_object(
BUCKET_NAME,
OBJECT_NAME,
TEMPLATE_FILE,
content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
)
# 生成访问URL相对路径用于数据库存储
# 根据接口文档file_path应该存储相对路径不包含存储桶名称
file_path = f"/{OBJECT_NAME}"
print(f"✓ 文件上传成功!")
print(f" 文件路径(相对): {file_path}")
return file_path
except S3Error as e:
print(f"MinIO错误: {e}")
raise
except Exception as e:
print(f"上传文件时发生错误: {e}")
raise
def update_database(file_path):
"""更新数据库中的文件路径"""
print("\n" + "="*60)
print("开始更新数据库...")
print("="*60)
try:
conn = pymysql.connect(**DB_CONFIG)
cursor = conn.cursor()
# 查找初步核实审批表的配置记录
select_sql = """
SELECT id, name, file_path
FROM f_polic_file_config
WHERE tenant_id = %s AND name = '初步核实审批表'
"""
cursor.execute(select_sql, (TENANT_ID,))
record = cursor.fetchone()
if not record:
print("错误: 未找到'初步核实审批表'的配置记录")
print("请先运行 init_preliminary_verification_fields.py 创建配置记录")
conn.close()
return False
record_id, record_name, old_path = record
print(f"找到配置记录:")
print(f" ID: {record_id}")
print(f" 名称: {record_name}")
print(f" 旧路径: {old_path}")
print(f" 新路径: {file_path}")
# 更新文件路径
update_sql = """
UPDATE f_polic_file_config
SET file_path = %s, updated_time = %s, updated_by = %s
WHERE id = %s AND tenant_id = %s
"""
cursor.execute(update_sql, (file_path, CURRENT_TIME, UPDATED_BY, record_id, TENANT_ID))
conn.commit()
print(f"\n✓ 数据库更新成功!")
# 验证更新
cursor.execute(select_sql, (TENANT_ID,))
updated_record = cursor.fetchone()
if updated_record and updated_record[2] == file_path:
print(f"✓ 验证成功: 文件路径已更新为 {updated_record[2]}")
else:
print(f"⚠ 警告: 验证失败,请手动检查")
conn.close()
return True
except Exception as e:
print(f"更新数据库时发生错误: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""主函数"""
try:
# 上传文件到MinIO
file_path = upload_to_minio()
# 更新数据库
success = update_database(file_path)
if success:
print("\n" + "="*60)
print("所有操作完成!")
print("="*60)
print(f"\n文件已上传到MinIO路径: {file_path}")
print(f"数据库已更新")
else:
print("\n" + "="*60)
print("操作完成,但数据库更新失败,请检查")
print("="*60)
except Exception as e:
print(f"\n错误: {e}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
main()

2
utils/__init__.py Normal file
View File

@ -0,0 +1,2 @@
# Utils package

Binary file not shown.

Binary file not shown.

34
utils/response.py Normal file
View File

@ -0,0 +1,34 @@
"""
响应工具 - 统一API响应格式
"""
from flask import jsonify
from datetime import datetime
def success_response(data=None, msg="ok"):
"""成功响应"""
return jsonify({
"code": 0,
"data": data,
"msg": msg,
"path": None,
"extra": None,
"timestamp": str(int(datetime.now().timestamp() * 1000)),
"errorMsg": "",
"isSuccess": True
})
def error_response(code, error_msg, data=None):
"""错误响应"""
return jsonify({
"code": code,
"data": data,
"msg": "error",
"path": None,
"extra": None,
"timestamp": str(int(datetime.now().timestamp() * 1000)),
"errorMsg": error_msg,
"isSuccess": False
}), 200 # 即使错误也返回200状态码错误信息在code字段中

View File

@ -0,0 +1 @@
pip

View File

@ -0,0 +1,7 @@
Copyright (C) 2016 Cory Dolphin, Olin College
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,147 @@
Metadata-Version: 2.1
Name: Flask-Cors
Version: 4.0.0
Summary: A Flask extension adding a decorator for CORS support
Home-page: https://github.com/corydolphin/flask-cors
Author: Cory Dolphin
Author-email: corydolphin@gmail.com
License: MIT
Platform: any
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Classifier: Topic :: Software Development :: Libraries :: Python Modules
License-File: LICENSE
Requires-Dist: Flask (>=0.9)
Flask-CORS
==========
|Build Status| |Latest Version| |Supported Python versions|
|License|
A Flask extension for handling Cross Origin Resource Sharing (CORS), making cross-origin AJAX possible.
This package has a simple philosophy: when you want to enable CORS, you wish to enable it for all use cases on a domain.
This means no mucking around with different allowed headers, methods, etc.
By default, submission of cookies across domains is disabled due to the security implications.
Please see the documentation for how to enable credential'ed requests, and please make sure you add some sort of `CSRF <http://en.wikipedia.org/wiki/Cross-site_request_forgery>`__ protection before doing so!
Installation
------------
Install the extension with using pip, or easy\_install.
.. code:: bash
$ pip install -U flask-cors
Usage
-----
This package exposes a Flask extension which by default enables CORS support on all routes, for all origins and methods.
It allows parameterization of all CORS headers on a per-resource level.
The package also contains a decorator, for those who prefer this approach.
Simple Usage
~~~~~~~~~~~~
In the simplest case, initialize the Flask-Cors extension with default arguments in order to allow CORS for all domains on all routes.
See the full list of options in the `documentation <https://flask-cors.corydolphin.com/en/latest/api.html#extension>`__.
.. code:: python
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/")
def helloWorld():
return "Hello, cross-origin-world!"
Resource specific CORS
^^^^^^^^^^^^^^^^^^^^^^
Alternatively, you can specify CORS options on a resource and origin level of granularity by passing a dictionary as the `resources` option, mapping paths to a set of options.
See the full list of options in the `documentation <https://flask-cors.corydolphin.com/en/latest/api.html#extension>`__.
.. code:: python
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
@app.route("/api/v1/users")
def list_users():
return "user example"
Route specific CORS via decorator
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This extension also exposes a simple decorator to decorate flask routes with.
Simply add ``@cross_origin()`` below a call to Flask's ``@app.route(..)`` to allow CORS on a given route.
See the full list of options in the `decorator documentation <https://flask-cors.corydolphin.com/en/latest/api.html#decorator>`__.
.. code:: python
@app.route("/")
@cross_origin()
def helloWorld():
return "Hello, cross-origin-world!"
Documentation
-------------
For a full list of options, please see the full `documentation <https://flask-cors.corydolphin.com/en/latest/api.html>`__
Troubleshooting
---------------
If things aren't working as you expect, enable logging to help understand what is going on under the hood, and why.
.. code:: python
logging.getLogger('flask_cors').level = logging.DEBUG
Tests
-----
A simple set of tests is included in ``test/``.
To run, install nose, and simply invoke ``nosetests`` or ``python setup.py test`` to exercise the tests.
If nosetests does not work for you, due to it no longer working with newer python versions.
You can use pytest to run the tests instead.
Contributing
------------
Questions, comments or improvements?
Please create an issue on `Github <https://github.com/corydolphin/flask-cors>`__, tweet at `@corydolphin <https://twitter.com/corydolphin>`__ or send me an email.
I do my best to include every contribution proposed in any way that I can.
Credits
-------
This Flask extension is based upon the `Decorator for the HTTP Access Control <https://web.archive.org/web/20190128010149/http://flask.pocoo.org/snippets/56/>`__ written by Armin Ronacher.
.. |Build Status| image:: https://api.travis-ci.org/corydolphin/flask-cors.svg?branch=master
:target: https://travis-ci.org/corydolphin/flask-cors
.. |Latest Version| image:: https://img.shields.io/pypi/v/Flask-Cors.svg
:target: https://pypi.python.org/pypi/Flask-Cors/
.. |Supported Python versions| image:: https://img.shields.io/pypi/pyversions/Flask-Cors.svg
:target: https://img.shields.io/pypi/pyversions/Flask-Cors.svg
.. |License| image:: http://img.shields.io/:license-mit-blue.svg
:target: https://pypi.python.org/pypi/Flask-Cors/

View File

@ -0,0 +1,17 @@
Flask_Cors-4.0.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
Flask_Cors-4.0.0.dist-info/LICENSE,sha256=bhob3FSDTB4HQMvOXV9vLK4chG_Sp_SCsRZJWU-vvV0,1069
Flask_Cors-4.0.0.dist-info/METADATA,sha256=iien2vLs6EIqceJgaNEJ6FPinwfjzFWSNl7XOkuyc10,5419
Flask_Cors-4.0.0.dist-info/RECORD,,
Flask_Cors-4.0.0.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
Flask_Cors-4.0.0.dist-info/WHEEL,sha256=a-zpFRIJzOq5QfuhBzbhiA1eHTzNCJn8OdRvhdNX0Rk,110
Flask_Cors-4.0.0.dist-info/top_level.txt,sha256=aWye_0QNZPp_QtPF4ZluLHqnyVLT9CPJsfiGhwqkWuo,11
flask_cors/__init__.py,sha256=wZDCvPTHspA2g1VV7KyKN7R-uCdBnirTlsCzgPDcQtI,792
flask_cors/__pycache__/__init__.cpython-312.pyc,,
flask_cors/__pycache__/core.cpython-312.pyc,,
flask_cors/__pycache__/decorator.cpython-312.pyc,,
flask_cors/__pycache__/extension.cpython-312.pyc,,
flask_cors/__pycache__/version.cpython-312.pyc,,
flask_cors/core.py,sha256=e1u_o5SOcS_gMWGjcQrkyk91uPICnzZ3AXZvy5jQ_FE,14063
flask_cors/decorator.py,sha256=BeJsyX1wYhVKWN04FAhb6z8YqffiRr7wKqwzHPap4bw,5009
flask_cors/extension.py,sha256=nP4Zq_BhgDVWwPdIl_f-uucNxD38pXUo-dkL-voXc58,7832
flask_cors/version.py,sha256=61rJjfThnbRdElpSP2tm31hPmFnHJmcwoPhtqA0Bi_Q,22

View File

@ -0,0 +1,6 @@
Wheel-Version: 1.0
Generator: bdist_wheel (0.40.0)
Root-Is-Purelib: true
Tag: py2-none-any
Tag: py3-none-any

View File

@ -0,0 +1 @@
flask_cors

Binary file not shown.

View File

@ -0,0 +1,33 @@
# This is a stub package designed to roughly emulate the _yaml
# extension module, which previously existed as a standalone module
# and has been moved into the `yaml` package namespace.
# It does not perfectly mimic its old counterpart, but should get
# close enough for anyone who's relying on it even when they shouldn't.
import yaml
# in some circumstances, the yaml module we imoprted may be from a different version, so we need
# to tread carefully when poking at it here (it may not have the attributes we expect)
if not getattr(yaml, '__with_libyaml__', False):
from sys import version_info
exc = ModuleNotFoundError if version_info >= (3, 6) else ImportError
raise exc("No module named '_yaml'")
else:
from yaml._yaml import *
import warnings
warnings.warn(
'The _yaml extension module is now located at yaml._yaml'
' and its location is subject to change. To use the'
' LibYAML-based parser and emitter, import from `yaml`:'
' `from yaml import CLoader as Loader, CDumper as Dumper`.',
DeprecationWarning
)
del warnings
# Don't `del yaml` here because yaml is actually an existing
# namespace member of _yaml.
__name__ = '_yaml'
# If the module is top-level (i.e. not a part of any specific package)
# then the attribute should be set to ''.
# https://docs.python.org/3.8/library/types.html
__package__ = ''

View File

@ -0,0 +1,104 @@
# SPDX-License-Identifier: MIT
"""
Classes Without Boilerplate
"""
from functools import partial
from typing import Callable, Literal, Protocol
from . import converters, exceptions, filters, setters, validators
from ._cmp import cmp_using
from ._config import get_run_validators, set_run_validators
from ._funcs import asdict, assoc, astuple, has, resolve_types
from ._make import (
NOTHING,
Attribute,
Converter,
Factory,
_Nothing,
attrib,
attrs,
evolve,
fields,
fields_dict,
make_class,
validate,
)
from ._next_gen import define, field, frozen, mutable
from ._version_info import VersionInfo
s = attributes = attrs
ib = attr = attrib
dataclass = partial(attrs, auto_attribs=True) # happy Easter ;)
class AttrsInstance(Protocol):
pass
NothingType = Literal[_Nothing.NOTHING]
__all__ = [
"NOTHING",
"Attribute",
"AttrsInstance",
"Converter",
"Factory",
"NothingType",
"asdict",
"assoc",
"astuple",
"attr",
"attrib",
"attributes",
"attrs",
"cmp_using",
"converters",
"define",
"evolve",
"exceptions",
"field",
"fields",
"fields_dict",
"filters",
"frozen",
"get_run_validators",
"has",
"ib",
"make_class",
"mutable",
"resolve_types",
"s",
"set_run_validators",
"setters",
"validate",
"validators",
]
def _make_getattr(mod_name: str) -> Callable:
"""
Create a metadata proxy for packaging information that uses *mod_name* in
its warnings and errors.
"""
def __getattr__(name: str) -> str:
if name not in ("__version__", "__version_info__"):
msg = f"module {mod_name} has no attribute {name}"
raise AttributeError(msg)
from importlib.metadata import metadata
meta = metadata("attrs")
if name == "__version_info__":
return VersionInfo._from_version_string(meta["version"])
return meta["version"]
return __getattr__
__getattr__ = _make_getattr(__name__)

View File

@ -0,0 +1,389 @@
import enum
import sys
from typing import (
Any,
Callable,
Generic,
Literal,
Mapping,
Protocol,
Sequence,
TypeVar,
overload,
)
# `import X as X` is required to make these public
from . import converters as converters
from . import exceptions as exceptions
from . import filters as filters
from . import setters as setters
from . import validators as validators
from ._cmp import cmp_using as cmp_using
from ._typing_compat import AttrsInstance_
from ._version_info import VersionInfo
from attrs import (
define as define,
field as field,
mutable as mutable,
frozen as frozen,
_EqOrderType,
_ValidatorType,
_ConverterType,
_ReprArgType,
_OnSetAttrType,
_OnSetAttrArgType,
_FieldTransformer,
_ValidatorArgType,
)
if sys.version_info >= (3, 10):
from typing import TypeGuard, TypeAlias
else:
from typing_extensions import TypeGuard, TypeAlias
if sys.version_info >= (3, 11):
from typing import dataclass_transform
else:
from typing_extensions import dataclass_transform
__version__: str
__version_info__: VersionInfo
__title__: str
__description__: str
__url__: str
__uri__: str
__author__: str
__email__: str
__license__: str
__copyright__: str
_T = TypeVar("_T")
_C = TypeVar("_C", bound=type)
_FilterType = Callable[["Attribute[_T]", _T], bool]
# We subclass this here to keep the protocol's qualified name clean.
class AttrsInstance(AttrsInstance_, Protocol):
pass
_A = TypeVar("_A", bound=type[AttrsInstance])
class _Nothing(enum.Enum):
NOTHING = enum.auto()
NOTHING = _Nothing.NOTHING
NothingType: TypeAlias = Literal[_Nothing.NOTHING]
# NOTE: Factory lies about its return type to make this possible:
# `x: List[int] # = Factory(list)`
# Work around mypy issue #4554 in the common case by using an overload.
@overload
def Factory(factory: Callable[[], _T]) -> _T: ...
@overload
def Factory(
factory: Callable[[Any], _T],
takes_self: Literal[True],
) -> _T: ...
@overload
def Factory(
factory: Callable[[], _T],
takes_self: Literal[False],
) -> _T: ...
In = TypeVar("In")
Out = TypeVar("Out")
class Converter(Generic[In, Out]):
@overload
def __init__(self, converter: Callable[[In], Out]) -> None: ...
@overload
def __init__(
self,
converter: Callable[[In, AttrsInstance, Attribute], Out],
*,
takes_self: Literal[True],
takes_field: Literal[True],
) -> None: ...
@overload
def __init__(
self,
converter: Callable[[In, Attribute], Out],
*,
takes_field: Literal[True],
) -> None: ...
@overload
def __init__(
self,
converter: Callable[[In, AttrsInstance], Out],
*,
takes_self: Literal[True],
) -> None: ...
class Attribute(Generic[_T]):
name: str
default: _T | None
validator: _ValidatorType[_T] | None
repr: _ReprArgType
cmp: _EqOrderType
eq: _EqOrderType
order: _EqOrderType
hash: bool | None
init: bool
converter: Converter | None
metadata: dict[Any, Any]
type: type[_T] | None
kw_only: bool
on_setattr: _OnSetAttrType
alias: str | None
def evolve(self, **changes: Any) -> "Attribute[Any]": ...
# NOTE: We had several choices for the annotation to use for type arg:
# 1) Type[_T]
# - Pros: Handles simple cases correctly
# - Cons: Might produce less informative errors in the case of conflicting
# TypeVars e.g. `attr.ib(default='bad', type=int)`
# 2) Callable[..., _T]
# - Pros: Better error messages than #1 for conflicting TypeVars
# - Cons: Terrible error messages for validator checks.
# e.g. attr.ib(type=int, validator=validate_str)
# -> error: Cannot infer function type argument
# 3) type (and do all of the work in the mypy plugin)
# - Pros: Simple here, and we could customize the plugin with our own errors.
# - Cons: Would need to write mypy plugin code to handle all the cases.
# We chose option #1.
# `attr` lies about its return type to make the following possible:
# attr() -> Any
# attr(8) -> int
# attr(validator=<some callable>) -> Whatever the callable expects.
# This makes this type of assignments possible:
# x: int = attr(8)
#
# This form catches explicit None or no default but with no other arguments
# returns Any.
@overload
def attrib(
default: None = ...,
validator: None = ...,
repr: _ReprArgType = ...,
cmp: _EqOrderType | None = ...,
hash: bool | None = ...,
init: bool = ...,
metadata: Mapping[Any, Any] | None = ...,
type: None = ...,
converter: None = ...,
factory: None = ...,
kw_only: bool | None = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
alias: str | None = ...,
) -> Any: ...
# This form catches an explicit None or no default and infers the type from the
# other arguments.
@overload
def attrib(
default: None = ...,
validator: _ValidatorArgType[_T] | None = ...,
repr: _ReprArgType = ...,
cmp: _EqOrderType | None = ...,
hash: bool | None = ...,
init: bool = ...,
metadata: Mapping[Any, Any] | None = ...,
type: type[_T] | None = ...,
converter: _ConverterType
| list[_ConverterType]
| tuple[_ConverterType]
| None = ...,
factory: Callable[[], _T] | None = ...,
kw_only: bool | None = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
alias: str | None = ...,
) -> _T: ...
# This form catches an explicit default argument.
@overload
def attrib(
default: _T,
validator: _ValidatorArgType[_T] | None = ...,
repr: _ReprArgType = ...,
cmp: _EqOrderType | None = ...,
hash: bool | None = ...,
init: bool = ...,
metadata: Mapping[Any, Any] | None = ...,
type: type[_T] | None = ...,
converter: _ConverterType
| list[_ConverterType]
| tuple[_ConverterType]
| None = ...,
factory: Callable[[], _T] | None = ...,
kw_only: bool | None = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
alias: str | None = ...,
) -> _T: ...
# This form covers type=non-Type: e.g. forward references (str), Any
@overload
def attrib(
default: _T | None = ...,
validator: _ValidatorArgType[_T] | None = ...,
repr: _ReprArgType = ...,
cmp: _EqOrderType | None = ...,
hash: bool | None = ...,
init: bool = ...,
metadata: Mapping[Any, Any] | None = ...,
type: object = ...,
converter: _ConverterType
| list[_ConverterType]
| tuple[_ConverterType]
| None = ...,
factory: Callable[[], _T] | None = ...,
kw_only: bool | None = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
alias: str | None = ...,
) -> Any: ...
@overload
@dataclass_transform(order_default=True, field_specifiers=(attrib, field))
def attrs(
maybe_cls: _C,
these: dict[str, Any] | None = ...,
repr_ns: str | None = ...,
repr: bool = ...,
cmp: _EqOrderType | None = ...,
hash: bool | None = ...,
init: bool = ...,
slots: bool = ...,
frozen: bool = ...,
weakref_slot: bool = ...,
str: bool = ...,
auto_attribs: bool = ...,
kw_only: bool = ...,
cache_hash: bool = ...,
auto_exc: bool = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
auto_detect: bool = ...,
collect_by_mro: bool = ...,
getstate_setstate: bool | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
field_transformer: _FieldTransformer | None = ...,
match_args: bool = ...,
unsafe_hash: bool | None = ...,
) -> _C: ...
@overload
@dataclass_transform(order_default=True, field_specifiers=(attrib, field))
def attrs(
maybe_cls: None = ...,
these: dict[str, Any] | None = ...,
repr_ns: str | None = ...,
repr: bool = ...,
cmp: _EqOrderType | None = ...,
hash: bool | None = ...,
init: bool = ...,
slots: bool = ...,
frozen: bool = ...,
weakref_slot: bool = ...,
str: bool = ...,
auto_attribs: bool = ...,
kw_only: bool = ...,
cache_hash: bool = ...,
auto_exc: bool = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
auto_detect: bool = ...,
collect_by_mro: bool = ...,
getstate_setstate: bool | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
field_transformer: _FieldTransformer | None = ...,
match_args: bool = ...,
unsafe_hash: bool | None = ...,
) -> Callable[[_C], _C]: ...
def fields(cls: type[AttrsInstance]) -> Any: ...
def fields_dict(cls: type[AttrsInstance]) -> dict[str, Attribute[Any]]: ...
def validate(inst: AttrsInstance) -> None: ...
def resolve_types(
cls: _A,
globalns: dict[str, Any] | None = ...,
localns: dict[str, Any] | None = ...,
attribs: list[Attribute[Any]] | None = ...,
include_extras: bool = ...,
) -> _A: ...
# TODO: add support for returning a proper attrs class from the mypy plugin
# we use Any instead of _CountingAttr so that e.g. `make_class('Foo',
# [attr.ib()])` is valid
def make_class(
name: str,
attrs: list[str] | tuple[str, ...] | dict[str, Any],
bases: tuple[type, ...] = ...,
class_body: dict[str, Any] | None = ...,
repr_ns: str | None = ...,
repr: bool = ...,
cmp: _EqOrderType | None = ...,
hash: bool | None = ...,
init: bool = ...,
slots: bool = ...,
frozen: bool = ...,
weakref_slot: bool = ...,
str: bool = ...,
auto_attribs: bool = ...,
kw_only: bool = ...,
cache_hash: bool = ...,
auto_exc: bool = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
collect_by_mro: bool = ...,
on_setattr: _OnSetAttrArgType | None = ...,
field_transformer: _FieldTransformer | None = ...,
) -> type: ...
# _funcs --
# TODO: add support for returning TypedDict from the mypy plugin
# FIXME: asdict/astuple do not honor their factory args. Waiting on one of
# these:
# https://github.com/python/mypy/issues/4236
# https://github.com/python/typing/issues/253
# XXX: remember to fix attrs.asdict/astuple too!
def asdict(
inst: AttrsInstance,
recurse: bool = ...,
filter: _FilterType[Any] | None = ...,
dict_factory: type[Mapping[Any, Any]] = ...,
retain_collection_types: bool = ...,
value_serializer: Callable[[type, Attribute[Any], Any], Any] | None = ...,
tuple_keys: bool | None = ...,
) -> dict[str, Any]: ...
# TODO: add support for returning NamedTuple from the mypy plugin
def astuple(
inst: AttrsInstance,
recurse: bool = ...,
filter: _FilterType[Any] | None = ...,
tuple_factory: type[Sequence[Any]] = ...,
retain_collection_types: bool = ...,
) -> tuple[Any, ...]: ...
def has(cls: type) -> TypeGuard[type[AttrsInstance]]: ...
def assoc(inst: _T, **changes: Any) -> _T: ...
def evolve(inst: _T, **changes: Any) -> _T: ...
# _config --
def set_run_validators(run: bool) -> None: ...
def get_run_validators() -> bool: ...
# aliases --
s = attributes = attrs
ib = attr = attrib
dataclass = attrs # Technically, partial(attrs, auto_attribs=True) ;)

View File

@ -0,0 +1,160 @@
# SPDX-License-Identifier: MIT
import functools
import types
from ._make import __ne__
_operation_names = {"eq": "==", "lt": "<", "le": "<=", "gt": ">", "ge": ">="}
def cmp_using(
eq=None,
lt=None,
le=None,
gt=None,
ge=None,
require_same_type=True,
class_name="Comparable",
):
"""
Create a class that can be passed into `attrs.field`'s ``eq``, ``order``,
and ``cmp`` arguments to customize field comparison.
The resulting class will have a full set of ordering methods if at least
one of ``{lt, le, gt, ge}`` and ``eq`` are provided.
Args:
eq (typing.Callable | None):
Callable used to evaluate equality of two objects.
lt (typing.Callable | None):
Callable used to evaluate whether one object is less than another
object.
le (typing.Callable | None):
Callable used to evaluate whether one object is less than or equal
to another object.
gt (typing.Callable | None):
Callable used to evaluate whether one object is greater than
another object.
ge (typing.Callable | None):
Callable used to evaluate whether one object is greater than or
equal to another object.
require_same_type (bool):
When `True`, equality and ordering methods will return
`NotImplemented` if objects are not of the same type.
class_name (str | None): Name of class. Defaults to "Comparable".
See `comparison` for more details.
.. versionadded:: 21.1.0
"""
body = {
"__slots__": ["value"],
"__init__": _make_init(),
"_requirements": [],
"_is_comparable_to": _is_comparable_to,
}
# Add operations.
num_order_functions = 0
has_eq_function = False
if eq is not None:
has_eq_function = True
body["__eq__"] = _make_operator("eq", eq)
body["__ne__"] = __ne__
if lt is not None:
num_order_functions += 1
body["__lt__"] = _make_operator("lt", lt)
if le is not None:
num_order_functions += 1
body["__le__"] = _make_operator("le", le)
if gt is not None:
num_order_functions += 1
body["__gt__"] = _make_operator("gt", gt)
if ge is not None:
num_order_functions += 1
body["__ge__"] = _make_operator("ge", ge)
type_ = types.new_class(
class_name, (object,), {}, lambda ns: ns.update(body)
)
# Add same type requirement.
if require_same_type:
type_._requirements.append(_check_same_type)
# Add total ordering if at least one operation was defined.
if 0 < num_order_functions < 4:
if not has_eq_function:
# functools.total_ordering requires __eq__ to be defined,
# so raise early error here to keep a nice stack.
msg = "eq must be define is order to complete ordering from lt, le, gt, ge."
raise ValueError(msg)
type_ = functools.total_ordering(type_)
return type_
def _make_init():
"""
Create __init__ method.
"""
def __init__(self, value):
"""
Initialize object with *value*.
"""
self.value = value
return __init__
def _make_operator(name, func):
"""
Create operator method.
"""
def method(self, other):
if not self._is_comparable_to(other):
return NotImplemented
result = func(self.value, other.value)
if result is NotImplemented:
return NotImplemented
return result
method.__name__ = f"__{name}__"
method.__doc__ = (
f"Return a {_operation_names[name]} b. Computed by attrs."
)
return method
def _is_comparable_to(self, other):
"""
Check whether `other` is comparable to `self`.
"""
return all(func(self, other) for func in self._requirements)
def _check_same_type(self, other):
"""
Return True if *self* and *other* are of the same type, False otherwise.
"""
return other.value.__class__ is self.value.__class__

View File

@ -0,0 +1,13 @@
from typing import Any, Callable
_CompareWithType = Callable[[Any, Any], bool]
def cmp_using(
eq: _CompareWithType | None = ...,
lt: _CompareWithType | None = ...,
le: _CompareWithType | None = ...,
gt: _CompareWithType | None = ...,
ge: _CompareWithType | None = ...,
require_same_type: bool = ...,
class_name: str = ...,
) -> type: ...

View File

@ -0,0 +1,99 @@
# SPDX-License-Identifier: MIT
import inspect
import platform
import sys
import threading
from collections.abc import Mapping, Sequence # noqa: F401
from typing import _GenericAlias
PYPY = platform.python_implementation() == "PyPy"
PY_3_10_PLUS = sys.version_info[:2] >= (3, 10)
PY_3_11_PLUS = sys.version_info[:2] >= (3, 11)
PY_3_12_PLUS = sys.version_info[:2] >= (3, 12)
PY_3_13_PLUS = sys.version_info[:2] >= (3, 13)
PY_3_14_PLUS = sys.version_info[:2] >= (3, 14)
if PY_3_14_PLUS:
import annotationlib
# We request forward-ref annotations to not break in the presence of
# forward references.
def _get_annotations(cls):
return annotationlib.get_annotations(
cls, format=annotationlib.Format.FORWARDREF
)
else:
def _get_annotations(cls):
"""
Get annotations for *cls*.
"""
return cls.__dict__.get("__annotations__", {})
class _AnnotationExtractor:
"""
Extract type annotations from a callable, returning None whenever there
is none.
"""
__slots__ = ["sig"]
def __init__(self, callable):
try:
self.sig = inspect.signature(callable)
except (ValueError, TypeError): # inspect failed
self.sig = None
def get_first_param_type(self):
"""
Return the type annotation of the first argument if it's not empty.
"""
if not self.sig:
return None
params = list(self.sig.parameters.values())
if params and params[0].annotation is not inspect.Parameter.empty:
return params[0].annotation
return None
def get_return_type(self):
"""
Return the return type if it's not empty.
"""
if (
self.sig
and self.sig.return_annotation is not inspect.Signature.empty
):
return self.sig.return_annotation
return None
# Thread-local global to track attrs instances which are already being repr'd.
# This is needed because there is no other (thread-safe) way to pass info
# about the instances that are already being repr'd through the call stack
# in order to ensure we don't perform infinite recursion.
#
# For instance, if an instance contains a dict which contains that instance,
# we need to know that we're already repr'ing the outside instance from within
# the dict's repr() call.
#
# This lives here rather than in _make.py so that the functions in _make.py
# don't have a direct reference to the thread-local in their globals dict.
# If they have such a reference, it breaks cloudpickle.
repr_context = threading.local()
def get_generic_base(cl):
"""If this is a generic class (A[str]), return the generic base for it."""
if cl.__class__ is _GenericAlias:
return cl.__origin__
return None

View File

@ -0,0 +1,31 @@
# SPDX-License-Identifier: MIT
__all__ = ["get_run_validators", "set_run_validators"]
_run_validators = True
def set_run_validators(run):
"""
Set whether or not validators are run. By default, they are run.
.. deprecated:: 21.3.0 It will not be removed, but it also will not be
moved to new ``attrs`` namespace. Use `attrs.validators.set_disabled()`
instead.
"""
if not isinstance(run, bool):
msg = "'run' must be bool."
raise TypeError(msg)
global _run_validators
_run_validators = run
def get_run_validators():
"""
Return whether or not validators are run.
.. deprecated:: 21.3.0 It will not be removed, but it also will not be
moved to new ``attrs`` namespace. Use `attrs.validators.get_disabled()`
instead.
"""
return _run_validators

View File

@ -0,0 +1,497 @@
# SPDX-License-Identifier: MIT
import copy
from ._compat import get_generic_base
from ._make import _OBJ_SETATTR, NOTHING, fields
from .exceptions import AttrsAttributeNotFoundError
_ATOMIC_TYPES = frozenset(
{
type(None),
bool,
int,
float,
str,
complex,
bytes,
type(...),
type,
range,
property,
}
)
def asdict(
inst,
recurse=True,
filter=None,
dict_factory=dict,
retain_collection_types=False,
value_serializer=None,
):
"""
Return the *attrs* attribute values of *inst* as a dict.
Optionally recurse into other *attrs*-decorated classes.
Args:
inst: Instance of an *attrs*-decorated class.
recurse (bool): Recurse into classes that are also *attrs*-decorated.
filter (~typing.Callable):
A callable whose return code determines whether an attribute or
element is included (`True`) or dropped (`False`). Is called with
the `attrs.Attribute` as the first argument and the value as the
second argument.
dict_factory (~typing.Callable):
A callable to produce dictionaries from. For example, to produce
ordered dictionaries instead of normal Python dictionaries, pass in
``collections.OrderedDict``.
retain_collection_types (bool):
Do not convert to `list` when encountering an attribute whose type
is `tuple` or `set`. Only meaningful if *recurse* is `True`.
value_serializer (typing.Callable | None):
A hook that is called for every attribute or dict key/value. It
receives the current instance, field and value and must return the
(updated) value. The hook is run *after* the optional *filter* has
been applied.
Returns:
Return type of *dict_factory*.
Raises:
attrs.exceptions.NotAnAttrsClassError:
If *cls* is not an *attrs* class.
.. versionadded:: 16.0.0 *dict_factory*
.. versionadded:: 16.1.0 *retain_collection_types*
.. versionadded:: 20.3.0 *value_serializer*
.. versionadded:: 21.3.0
If a dict has a collection for a key, it is serialized as a tuple.
"""
attrs = fields(inst.__class__)
rv = dict_factory()
for a in attrs:
v = getattr(inst, a.name)
if filter is not None and not filter(a, v):
continue
if value_serializer is not None:
v = value_serializer(inst, a, v)
if recurse is True:
value_type = type(v)
if value_type in _ATOMIC_TYPES:
rv[a.name] = v
elif has(value_type):
rv[a.name] = asdict(
v,
recurse=True,
filter=filter,
dict_factory=dict_factory,
retain_collection_types=retain_collection_types,
value_serializer=value_serializer,
)
elif issubclass(value_type, (tuple, list, set, frozenset)):
cf = value_type if retain_collection_types is True else list
items = [
_asdict_anything(
i,
is_key=False,
filter=filter,
dict_factory=dict_factory,
retain_collection_types=retain_collection_types,
value_serializer=value_serializer,
)
for i in v
]
try:
rv[a.name] = cf(items)
except TypeError:
if not issubclass(cf, tuple):
raise
# Workaround for TypeError: cf.__new__() missing 1 required
# positional argument (which appears, for a namedturle)
rv[a.name] = cf(*items)
elif issubclass(value_type, dict):
df = dict_factory
rv[a.name] = df(
(
_asdict_anything(
kk,
is_key=True,
filter=filter,
dict_factory=df,
retain_collection_types=retain_collection_types,
value_serializer=value_serializer,
),
_asdict_anything(
vv,
is_key=False,
filter=filter,
dict_factory=df,
retain_collection_types=retain_collection_types,
value_serializer=value_serializer,
),
)
for kk, vv in v.items()
)
else:
rv[a.name] = v
else:
rv[a.name] = v
return rv
def _asdict_anything(
val,
is_key,
filter,
dict_factory,
retain_collection_types,
value_serializer,
):
"""
``asdict`` only works on attrs instances, this works on anything.
"""
val_type = type(val)
if val_type in _ATOMIC_TYPES:
rv = val
if value_serializer is not None:
rv = value_serializer(None, None, rv)
elif getattr(val_type, "__attrs_attrs__", None) is not None:
# Attrs class.
rv = asdict(
val,
recurse=True,
filter=filter,
dict_factory=dict_factory,
retain_collection_types=retain_collection_types,
value_serializer=value_serializer,
)
elif issubclass(val_type, (tuple, list, set, frozenset)):
if retain_collection_types is True:
cf = val.__class__
elif is_key:
cf = tuple
else:
cf = list
rv = cf(
[
_asdict_anything(
i,
is_key=False,
filter=filter,
dict_factory=dict_factory,
retain_collection_types=retain_collection_types,
value_serializer=value_serializer,
)
for i in val
]
)
elif issubclass(val_type, dict):
df = dict_factory
rv = df(
(
_asdict_anything(
kk,
is_key=True,
filter=filter,
dict_factory=df,
retain_collection_types=retain_collection_types,
value_serializer=value_serializer,
),
_asdict_anything(
vv,
is_key=False,
filter=filter,
dict_factory=df,
retain_collection_types=retain_collection_types,
value_serializer=value_serializer,
),
)
for kk, vv in val.items()
)
else:
rv = val
if value_serializer is not None:
rv = value_serializer(None, None, rv)
return rv
def astuple(
inst,
recurse=True,
filter=None,
tuple_factory=tuple,
retain_collection_types=False,
):
"""
Return the *attrs* attribute values of *inst* as a tuple.
Optionally recurse into other *attrs*-decorated classes.
Args:
inst: Instance of an *attrs*-decorated class.
recurse (bool):
Recurse into classes that are also *attrs*-decorated.
filter (~typing.Callable):
A callable whose return code determines whether an attribute or
element is included (`True`) or dropped (`False`). Is called with
the `attrs.Attribute` as the first argument and the value as the
second argument.
tuple_factory (~typing.Callable):
A callable to produce tuples from. For example, to produce lists
instead of tuples.
retain_collection_types (bool):
Do not convert to `list` or `dict` when encountering an attribute
which type is `tuple`, `dict` or `set`. Only meaningful if
*recurse* is `True`.
Returns:
Return type of *tuple_factory*
Raises:
attrs.exceptions.NotAnAttrsClassError:
If *cls* is not an *attrs* class.
.. versionadded:: 16.2.0
"""
attrs = fields(inst.__class__)
rv = []
retain = retain_collection_types # Very long. :/
for a in attrs:
v = getattr(inst, a.name)
if filter is not None and not filter(a, v):
continue
value_type = type(v)
if recurse is True:
if value_type in _ATOMIC_TYPES:
rv.append(v)
elif has(value_type):
rv.append(
astuple(
v,
recurse=True,
filter=filter,
tuple_factory=tuple_factory,
retain_collection_types=retain,
)
)
elif issubclass(value_type, (tuple, list, set, frozenset)):
cf = v.__class__ if retain is True else list
items = [
(
astuple(
j,
recurse=True,
filter=filter,
tuple_factory=tuple_factory,
retain_collection_types=retain,
)
if has(j.__class__)
else j
)
for j in v
]
try:
rv.append(cf(items))
except TypeError:
if not issubclass(cf, tuple):
raise
# Workaround for TypeError: cf.__new__() missing 1 required
# positional argument (which appears, for a namedturle)
rv.append(cf(*items))
elif issubclass(value_type, dict):
df = value_type if retain is True else dict
rv.append(
df(
(
(
astuple(
kk,
tuple_factory=tuple_factory,
retain_collection_types=retain,
)
if has(kk.__class__)
else kk
),
(
astuple(
vv,
tuple_factory=tuple_factory,
retain_collection_types=retain,
)
if has(vv.__class__)
else vv
),
)
for kk, vv in v.items()
)
)
else:
rv.append(v)
else:
rv.append(v)
return rv if tuple_factory is list else tuple_factory(rv)
def has(cls):
"""
Check whether *cls* is a class with *attrs* attributes.
Args:
cls (type): Class to introspect.
Raises:
TypeError: If *cls* is not a class.
Returns:
bool:
"""
attrs = getattr(cls, "__attrs_attrs__", None)
if attrs is not None:
return True
# No attrs, maybe it's a specialized generic (A[str])?
generic_base = get_generic_base(cls)
if generic_base is not None:
generic_attrs = getattr(generic_base, "__attrs_attrs__", None)
if generic_attrs is not None:
# Stick it on here for speed next time.
cls.__attrs_attrs__ = generic_attrs
return generic_attrs is not None
return False
def assoc(inst, **changes):
"""
Copy *inst* and apply *changes*.
This is different from `evolve` that applies the changes to the arguments
that create the new instance.
`evolve`'s behavior is preferable, but there are `edge cases`_ where it
doesn't work. Therefore `assoc` is deprecated, but will not be removed.
.. _`edge cases`: https://github.com/python-attrs/attrs/issues/251
Args:
inst: Instance of a class with *attrs* attributes.
changes: Keyword changes in the new copy.
Returns:
A copy of inst with *changes* incorporated.
Raises:
attrs.exceptions.AttrsAttributeNotFoundError:
If *attr_name* couldn't be found on *cls*.
attrs.exceptions.NotAnAttrsClassError:
If *cls* is not an *attrs* class.
.. deprecated:: 17.1.0
Use `attrs.evolve` instead if you can. This function will not be
removed du to the slightly different approach compared to
`attrs.evolve`, though.
"""
new = copy.copy(inst)
attrs = fields(inst.__class__)
for k, v in changes.items():
a = getattr(attrs, k, NOTHING)
if a is NOTHING:
msg = f"{k} is not an attrs attribute on {new.__class__}."
raise AttrsAttributeNotFoundError(msg)
_OBJ_SETATTR(new, k, v)
return new
def resolve_types(
cls, globalns=None, localns=None, attribs=None, include_extras=True
):
"""
Resolve any strings and forward annotations in type annotations.
This is only required if you need concrete types in :class:`Attribute`'s
*type* field. In other words, you don't need to resolve your types if you
only use them for static type checking.
With no arguments, names will be looked up in the module in which the class
was created. If this is not what you want, for example, if the name only
exists inside a method, you may pass *globalns* or *localns* to specify
other dictionaries in which to look up these names. See the docs of
`typing.get_type_hints` for more details.
Args:
cls (type): Class to resolve.
globalns (dict | None): Dictionary containing global variables.
localns (dict | None): Dictionary containing local variables.
attribs (list | None):
List of attribs for the given class. This is necessary when calling
from inside a ``field_transformer`` since *cls* is not an *attrs*
class yet.
include_extras (bool):
Resolve more accurately, if possible. Pass ``include_extras`` to
``typing.get_hints``, if supported by the typing module. On
supported Python versions (3.9+), this resolves the types more
accurately.
Raises:
TypeError: If *cls* is not a class.
attrs.exceptions.NotAnAttrsClassError:
If *cls* is not an *attrs* class and you didn't pass any attribs.
NameError: If types cannot be resolved because of missing variables.
Returns:
*cls* so you can use this function also as a class decorator. Please
note that you have to apply it **after** `attrs.define`. That means the
decorator has to come in the line **before** `attrs.define`.
.. versionadded:: 20.1.0
.. versionadded:: 21.1.0 *attribs*
.. versionadded:: 23.1.0 *include_extras*
"""
# Since calling get_type_hints is expensive we cache whether we've
# done it already.
if getattr(cls, "__attrs_types_resolved__", None) != cls:
import typing
kwargs = {
"globalns": globalns,
"localns": localns,
"include_extras": include_extras,
}
hints = typing.get_type_hints(cls, **kwargs)
for field in fields(cls) if attribs is None else attribs:
if field.name in hints:
# Since fields have been frozen we must work around it.
_OBJ_SETATTR(field, "type", hints[field.name])
# We store the class we resolved so that subclasses know they haven't
# been resolved.
cls.__attrs_types_resolved__ = cls
# Return the class so you can use it as a decorator too.
return cls

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,674 @@
# SPDX-License-Identifier: MIT
"""
These are keyword-only APIs that call `attr.s` and `attr.ib` with different
default values.
"""
from functools import partial
from . import setters
from ._funcs import asdict as _asdict
from ._funcs import astuple as _astuple
from ._make import (
_DEFAULT_ON_SETATTR,
NOTHING,
_frozen_setattrs,
attrib,
attrs,
)
from .exceptions import NotAnAttrsClassError, UnannotatedAttributeError
def define(
maybe_cls=None,
*,
these=None,
repr=None,
unsafe_hash=None,
hash=None,
init=None,
slots=True,
frozen=False,
weakref_slot=True,
str=False,
auto_attribs=None,
kw_only=False,
cache_hash=False,
auto_exc=True,
eq=None,
order=False,
auto_detect=True,
getstate_setstate=None,
on_setattr=None,
field_transformer=None,
match_args=True,
force_kw_only=False,
):
r"""
A class decorator that adds :term:`dunder methods` according to
:term:`fields <field>` specified using :doc:`type annotations <types>`,
`field()` calls, or the *these* argument.
Since *attrs* patches or replaces an existing class, you cannot use
`object.__init_subclass__` with *attrs* classes, because it runs too early.
As a replacement, you can define ``__attrs_init_subclass__`` on your class.
It will be called by *attrs* classes that subclass it after they're
created. See also :ref:`init-subclass`.
Args:
slots (bool):
Create a :term:`slotted class <slotted classes>` that's more
memory-efficient. Slotted classes are generally superior to the
default dict classes, but have some gotchas you should know about,
so we encourage you to read the :term:`glossary entry <slotted
classes>`.
auto_detect (bool):
Instead of setting the *init*, *repr*, *eq*, and *hash* arguments
explicitly, assume they are set to True **unless any** of the
involved methods for one of the arguments is implemented in the
*current* class (meaning, it is *not* inherited from some base
class).
So, for example by implementing ``__eq__`` on a class yourself,
*attrs* will deduce ``eq=False`` and will create *neither*
``__eq__`` *nor* ``__ne__`` (but Python classes come with a
sensible ``__ne__`` by default, so it *should* be enough to only
implement ``__eq__`` in most cases).
Passing :data:`True` or :data:`False` to *init*, *repr*, *eq*, or *hash*
overrides whatever *auto_detect* would determine.
auto_exc (bool):
If the class subclasses `BaseException` (which implicitly includes
any subclass of any exception), the following happens to behave
like a well-behaved Python exception class:
- the values for *eq*, *order*, and *hash* are ignored and the
instances compare and hash by the instance's ids [#]_ ,
- all attributes that are either passed into ``__init__`` or have a
default value are additionally available as a tuple in the
``args`` attribute,
- the value of *str* is ignored leaving ``__str__`` to base
classes.
.. [#]
Note that *attrs* will *not* remove existing implementations of
``__hash__`` or the equality methods. It just won't add own
ones.
on_setattr (~typing.Callable | list[~typing.Callable] | None | ~typing.Literal[attrs.setters.NO_OP]):
A callable that is run whenever the user attempts to set an
attribute (either by assignment like ``i.x = 42`` or by using
`setattr` like ``setattr(i, "x", 42)``). It receives the same
arguments as validators: the instance, the attribute that is being
modified, and the new value.
If no exception is raised, the attribute is set to the return value
of the callable.
If a list of callables is passed, they're automatically wrapped in
an `attrs.setters.pipe`.
If left None, the default behavior is to run converters and
validators whenever an attribute is set.
init (bool):
Create a ``__init__`` method that initializes the *attrs*
attributes. Leading underscores are stripped for the argument name,
unless an alias is set on the attribute.
.. seealso::
`init` shows advanced ways to customize the generated
``__init__`` method, including executing code before and after.
repr(bool):
Create a ``__repr__`` method with a human readable representation
of *attrs* attributes.
str (bool):
Create a ``__str__`` method that is identical to ``__repr__``. This
is usually not necessary except for `Exception`\ s.
eq (bool | None):
If True or None (default), add ``__eq__`` and ``__ne__`` methods
that check two instances for equality.
.. seealso::
`comparison` describes how to customize the comparison behavior
going as far comparing NumPy arrays.
order (bool | None):
If True, add ``__lt__``, ``__le__``, ``__gt__``, and ``__ge__``
methods that behave like *eq* above and allow instances to be
ordered.
They compare the instances as if they were tuples of their *attrs*
attributes if and only if the types of both classes are
*identical*.
If `None` mirror value of *eq*.
.. seealso:: `comparison`
unsafe_hash (bool | None):
If None (default), the ``__hash__`` method is generated according
how *eq* and *frozen* are set.
1. If *both* are True, *attrs* will generate a ``__hash__`` for
you.
2. If *eq* is True and *frozen* is False, ``__hash__`` will be set
to None, marking it unhashable (which it is).
3. If *eq* is False, ``__hash__`` will be left untouched meaning
the ``__hash__`` method of the base class will be used. If the
base class is `object`, this means it will fall back to id-based
hashing.
Although not recommended, you can decide for yourself and force
*attrs* to create one (for example, if the class is immutable even
though you didn't freeze it programmatically) by passing True or
not. Both of these cases are rather special and should be used
carefully.
.. seealso::
- Our documentation on `hashing`,
- Python's documentation on `object.__hash__`,
- and the `GitHub issue that led to the default \ behavior
<https://github.com/python-attrs/attrs/issues/136>`_ for more
details.
hash (bool | None):
Deprecated alias for *unsafe_hash*. *unsafe_hash* takes precedence.
cache_hash (bool):
Ensure that the object's hash code is computed only once and stored
on the object. If this is set to True, hashing must be either
explicitly or implicitly enabled for this class. If the hash code
is cached, avoid any reassignments of fields involved in hash code
computation or mutations of the objects those fields point to after
object creation. If such changes occur, the behavior of the
object's hash code is undefined.
frozen (bool):
Make instances immutable after initialization. If someone attempts
to modify a frozen instance, `attrs.exceptions.FrozenInstanceError`
is raised.
.. note::
1. This is achieved by installing a custom ``__setattr__``
method on your class, so you can't implement your own.
2. True immutability is impossible in Python.
3. This *does* have a minor a runtime performance `impact
<how-frozen>` when initializing new instances. In other
words: ``__init__`` is slightly slower with ``frozen=True``.
4. If a class is frozen, you cannot modify ``self`` in
``__attrs_post_init__`` or a self-written ``__init__``. You
can circumvent that limitation by using
``object.__setattr__(self, "attribute_name", value)``.
5. Subclasses of a frozen class are frozen too.
kw_only (bool):
Make attributes keyword-only in the generated ``__init__`` (if
*init* is False, this parameter is ignored). Attributes that
explicitly set ``kw_only=False`` are not affected; base class
attributes are also not affected.
Also see *force_kw_only*.
weakref_slot (bool):
Make instances weak-referenceable. This has no effect unless
*slots* is True.
field_transformer (~typing.Callable | None):
A function that is called with the original class object and all
fields right before *attrs* finalizes the class. You can use this,
for example, to automatically add converters or validators to
fields based on their types.
.. seealso:: `transform-fields`
match_args (bool):
If True (default), set ``__match_args__`` on the class to support
:pep:`634` (*Structural Pattern Matching*). It is a tuple of all
non-keyword-only ``__init__`` parameter names on Python 3.10 and
later. Ignored on older Python versions.
collect_by_mro (bool):
If True, *attrs* collects attributes from base classes correctly
according to the `method resolution order
<https://docs.python.org/3/howto/mro.html>`_. If False, *attrs*
will mimic the (wrong) behavior of `dataclasses` and :pep:`681`.
See also `issue #428
<https://github.com/python-attrs/attrs/issues/428>`_.
force_kw_only (bool):
A back-compat flag for restoring pre-25.4.0 behavior. If True and
``kw_only=True``, all attributes are made keyword-only, including
base class attributes, and those set to ``kw_only=False`` at the
attribute level. Defaults to False.
See also `issue #980
<https://github.com/python-attrs/attrs/issues/980>`_.
getstate_setstate (bool | None):
.. note::
This is usually only interesting for slotted classes and you
should probably just set *auto_detect* to True.
If True, ``__getstate__`` and ``__setstate__`` are generated and
attached to the class. This is necessary for slotted classes to be
pickleable. If left None, it's True by default for slotted classes
and False for dict classes.
If *auto_detect* is True, and *getstate_setstate* is left None, and
**either** ``__getstate__`` or ``__setstate__`` is detected
directly on the class (meaning: not inherited), it is set to False
(this is usually what you want).
auto_attribs (bool | None):
If True, look at type annotations to determine which attributes to
use, like `dataclasses`. If False, it will only look for explicit
:func:`field` class attributes, like classic *attrs*.
If left None, it will guess:
1. If any attributes are annotated and no unannotated
`attrs.field`\ s are found, it assumes *auto_attribs=True*.
2. Otherwise it assumes *auto_attribs=False* and tries to collect
`attrs.field`\ s.
If *attrs* decides to look at type annotations, **all** fields
**must** be annotated. If *attrs* encounters a field that is set to
a :func:`field` / `attr.ib` but lacks a type annotation, an
`attrs.exceptions.UnannotatedAttributeError` is raised. Use
``field_name: typing.Any = field(...)`` if you don't want to set a
type.
.. warning::
For features that use the attribute name to create decorators
(for example, :ref:`validators <validators>`), you still *must*
assign :func:`field` / `attr.ib` to them. Otherwise Python will
either not find the name or try to use the default value to
call, for example, ``validator`` on it.
Attributes annotated as `typing.ClassVar`, and attributes that are
neither annotated nor set to an `field()` are **ignored**.
these (dict[str, object]):
A dictionary of name to the (private) return value of `field()`
mappings. This is useful to avoid the definition of your attributes
within the class body because you can't (for example, if you want
to add ``__repr__`` methods to Django models) or don't want to.
If *these* is not `None`, *attrs* will *not* search the class body
for attributes and will *not* remove any attributes from it.
The order is deduced from the order of the attributes inside
*these*.
Arguably, this is a rather obscure feature.
.. versionadded:: 20.1.0
.. versionchanged:: 21.3.0 Converters are also run ``on_setattr``.
.. versionadded:: 22.2.0
*unsafe_hash* as an alias for *hash* (for :pep:`681` compliance).
.. versionchanged:: 24.1.0
Instances are not compared as tuples of attributes anymore, but using a
big ``and`` condition. This is faster and has more correct behavior for
uncomparable values like `math.nan`.
.. versionadded:: 24.1.0
If a class has an *inherited* classmethod called
``__attrs_init_subclass__``, it is executed after the class is created.
.. deprecated:: 24.1.0 *hash* is deprecated in favor of *unsafe_hash*.
.. versionadded:: 24.3.0
Unless already present, a ``__replace__`` method is automatically
created for `copy.replace` (Python 3.13+ only).
.. versionchanged:: 25.4.0
*kw_only* now only applies to attributes defined in the current class,
and respects attribute-level ``kw_only=False`` settings.
.. versionadded:: 25.4.0
Added *force_kw_only* to go back to the previous *kw_only* behavior.
.. note::
The main differences to the classic `attr.s` are:
- Automatically detect whether or not *auto_attribs* should be `True`
(c.f. *auto_attribs* parameter).
- Converters and validators run when attributes are set by default --
if *frozen* is `False`.
- *slots=True*
Usually, this has only upsides and few visible effects in everyday
programming. But it *can* lead to some surprising behaviors, so
please make sure to read :term:`slotted classes`.
- *auto_exc=True*
- *auto_detect=True*
- *order=False*
- *force_kw_only=False*
- Some options that were only relevant on Python 2 or were kept around
for backwards-compatibility have been removed.
"""
def do_it(cls, auto_attribs):
return attrs(
maybe_cls=cls,
these=these,
repr=repr,
hash=hash,
unsafe_hash=unsafe_hash,
init=init,
slots=slots,
frozen=frozen,
weakref_slot=weakref_slot,
str=str,
auto_attribs=auto_attribs,
kw_only=kw_only,
cache_hash=cache_hash,
auto_exc=auto_exc,
eq=eq,
order=order,
auto_detect=auto_detect,
collect_by_mro=True,
getstate_setstate=getstate_setstate,
on_setattr=on_setattr,
field_transformer=field_transformer,
match_args=match_args,
force_kw_only=force_kw_only,
)
def wrap(cls):
"""
Making this a wrapper ensures this code runs during class creation.
We also ensure that frozen-ness of classes is inherited.
"""
nonlocal frozen, on_setattr
had_on_setattr = on_setattr not in (None, setters.NO_OP)
# By default, mutable classes convert & validate on setattr.
if frozen is False and on_setattr is None:
on_setattr = _DEFAULT_ON_SETATTR
# However, if we subclass a frozen class, we inherit the immutability
# and disable on_setattr.
for base_cls in cls.__bases__:
if base_cls.__setattr__ is _frozen_setattrs:
if had_on_setattr:
msg = "Frozen classes can't use on_setattr (frozen-ness was inherited)."
raise ValueError(msg)
on_setattr = setters.NO_OP
break
if auto_attribs is not None:
return do_it(cls, auto_attribs)
try:
return do_it(cls, True)
except UnannotatedAttributeError:
return do_it(cls, False)
# maybe_cls's type depends on the usage of the decorator. It's a class
# if it's used as `@attrs` but `None` if used as `@attrs()`.
if maybe_cls is None:
return wrap
return wrap(maybe_cls)
mutable = define
frozen = partial(define, frozen=True, on_setattr=None)
def field(
*,
default=NOTHING,
validator=None,
repr=True,
hash=None,
init=True,
metadata=None,
type=None,
converter=None,
factory=None,
kw_only=None,
eq=None,
order=None,
on_setattr=None,
alias=None,
):
"""
Create a new :term:`field` / :term:`attribute` on a class.
.. warning::
Does **nothing** unless the class is also decorated with
`attrs.define` (or similar)!
Args:
default:
A value that is used if an *attrs*-generated ``__init__`` is used
and no value is passed while instantiating or the attribute is
excluded using ``init=False``.
If the value is an instance of `attrs.Factory`, its callable will
be used to construct a new value (useful for mutable data types
like lists or dicts).
If a default is not set (or set manually to `attrs.NOTHING`), a
value *must* be supplied when instantiating; otherwise a
`TypeError` will be raised.
.. seealso:: `defaults`
factory (~typing.Callable):
Syntactic sugar for ``default=attr.Factory(factory)``.
validator (~typing.Callable | list[~typing.Callable]):
Callable that is called by *attrs*-generated ``__init__`` methods
after the instance has been initialized. They receive the
initialized instance, the :func:`~attrs.Attribute`, and the passed
value.
The return value is *not* inspected so the validator has to throw
an exception itself.
If a `list` is passed, its items are treated as validators and must
all pass.
Validators can be globally disabled and re-enabled using
`attrs.validators.get_disabled` / `attrs.validators.set_disabled`.
The validator can also be set using decorator notation as shown
below.
.. seealso:: :ref:`validators`
repr (bool | ~typing.Callable):
Include this attribute in the generated ``__repr__`` method. If
True, include the attribute; if False, omit it. By default, the
built-in ``repr()`` function is used. To override how the attribute
value is formatted, pass a ``callable`` that takes a single value
and returns a string. Note that the resulting string is used as-is,
which means it will be used directly *instead* of calling
``repr()`` (the default).
eq (bool | ~typing.Callable):
If True (default), include this attribute in the generated
``__eq__`` and ``__ne__`` methods that check two instances for
equality. To override how the attribute value is compared, pass a
callable that takes a single value and returns the value to be
compared.
.. seealso:: `comparison`
order (bool | ~typing.Callable):
If True (default), include this attributes in the generated
``__lt__``, ``__le__``, ``__gt__`` and ``__ge__`` methods. To
override how the attribute value is ordered, pass a callable that
takes a single value and returns the value to be ordered.
.. seealso:: `comparison`
hash (bool | None):
Include this attribute in the generated ``__hash__`` method. If
None (default), mirror *eq*'s value. This is the correct behavior
according the Python spec. Setting this value to anything else
than None is *discouraged*.
.. seealso:: `hashing`
init (bool):
Include this attribute in the generated ``__init__`` method.
It is possible to set this to False and set a default value. In
that case this attributed is unconditionally initialized with the
specified default value or factory.
.. seealso:: `init`
converter (typing.Callable | Converter):
A callable that is called by *attrs*-generated ``__init__`` methods
to convert attribute's value to the desired format.
If a vanilla callable is passed, it is given the passed-in value as
the only positional argument. It is possible to receive additional
arguments by wrapping the callable in a `Converter`.
Either way, the returned value will be used as the new value of the
attribute. The value is converted before being passed to the
validator, if any.
.. seealso:: :ref:`converters`
metadata (dict | None):
An arbitrary mapping, to be used by third-party code.
.. seealso:: `extending-metadata`.
type (type):
The type of the attribute. Nowadays, the preferred method to
specify the type is using a variable annotation (see :pep:`526`).
This argument is provided for backwards-compatibility and for usage
with `make_class`. Regardless of the approach used, the type will
be stored on ``Attribute.type``.
Please note that *attrs* doesn't do anything with this metadata by
itself. You can use it as part of your own code or for `static type
checking <types>`.
kw_only (bool | None):
Make this attribute keyword-only in the generated ``__init__`` (if
*init* is False, this parameter is ignored). If None (default),
mirror the setting from `attrs.define`.
on_setattr (~typing.Callable | list[~typing.Callable] | None | ~typing.Literal[attrs.setters.NO_OP]):
Allows to overwrite the *on_setattr* setting from `attr.s`. If left
None, the *on_setattr* value from `attr.s` is used. Set to
`attrs.setters.NO_OP` to run **no** `setattr` hooks for this
attribute -- regardless of the setting in `define()`.
alias (str | None):
Override this attribute's parameter name in the generated
``__init__`` method. If left None, default to ``name`` stripped
of leading underscores. See `private-attributes`.
.. versionadded:: 20.1.0
.. versionchanged:: 21.1.0
*eq*, *order*, and *cmp* also accept a custom callable
.. versionadded:: 22.2.0 *alias*
.. versionadded:: 23.1.0
The *type* parameter has been re-added; mostly for `attrs.make_class`.
Please note that type checkers ignore this metadata.
.. versionchanged:: 25.4.0
*kw_only* can now be None, and its default is also changed from False to
None.
.. seealso::
`attr.ib`
"""
return attrib(
default=default,
validator=validator,
repr=repr,
hash=hash,
init=init,
metadata=metadata,
type=type,
converter=converter,
factory=factory,
kw_only=kw_only,
eq=eq,
order=order,
on_setattr=on_setattr,
alias=alias,
)
def asdict(inst, *, recurse=True, filter=None, value_serializer=None):
"""
Same as `attr.asdict`, except that collections types are always retained
and dict is always used as *dict_factory*.
.. versionadded:: 21.3.0
"""
return _asdict(
inst=inst,
recurse=recurse,
filter=filter,
value_serializer=value_serializer,
retain_collection_types=True,
)
def astuple(inst, *, recurse=True, filter=None):
"""
Same as `attr.astuple`, except that collections types are always retained
and `tuple` is always used as the *tuple_factory*.
.. versionadded:: 21.3.0
"""
return _astuple(
inst=inst, recurse=recurse, filter=filter, retain_collection_types=True
)
def inspect(cls):
"""
Inspect the class and return its effective build parameters.
Warning:
This feature is currently **experimental** and is not covered by our
strict backwards-compatibility guarantees.
Args:
cls: The *attrs*-decorated class to inspect.
Returns:
The effective build parameters of the class.
Raises:
NotAnAttrsClassError: If the class is not an *attrs*-decorated class.
.. versionadded:: 25.4.0
"""
try:
return cls.__dict__["__attrs_props__"]
except KeyError:
msg = f"{cls!r} is not an attrs-decorated class."
raise NotAnAttrsClassError(msg) from None

View File

@ -0,0 +1,15 @@
from typing import Any, ClassVar, Protocol
# MYPY is a special constant in mypy which works the same way as `TYPE_CHECKING`.
MYPY = False
if MYPY:
# A protocol to be able to statically accept an attrs class.
class AttrsInstance_(Protocol):
__attrs_attrs__: ClassVar[Any]
else:
# For type checkers without plug-in support use an empty protocol that
# will (hopefully) be combined into a union.
class AttrsInstance_(Protocol):
pass

View File

@ -0,0 +1,89 @@
# SPDX-License-Identifier: MIT
from functools import total_ordering
from ._funcs import astuple
from ._make import attrib, attrs
@total_ordering
@attrs(eq=False, order=False, slots=True, frozen=True)
class VersionInfo:
"""
A version object that can be compared to tuple of length 1--4:
>>> attr.VersionInfo(19, 1, 0, "final") <= (19, 2)
True
>>> attr.VersionInfo(19, 1, 0, "final") < (19, 1, 1)
True
>>> vi = attr.VersionInfo(19, 2, 0, "final")
>>> vi < (19, 1, 1)
False
>>> vi < (19,)
False
>>> vi == (19, 2,)
True
>>> vi == (19, 2, 1)
False
.. versionadded:: 19.2
"""
year = attrib(type=int)
minor = attrib(type=int)
micro = attrib(type=int)
releaselevel = attrib(type=str)
@classmethod
def _from_version_string(cls, s):
"""
Parse *s* and return a _VersionInfo.
"""
v = s.split(".")
if len(v) == 3:
v.append("final")
return cls(
year=int(v[0]), minor=int(v[1]), micro=int(v[2]), releaselevel=v[3]
)
def _ensure_tuple(self, other):
"""
Ensure *other* is a tuple of a valid length.
Returns a possibly transformed *other* and ourselves as a tuple of
the same length as *other*.
"""
if self.__class__ is other.__class__:
other = astuple(other)
if not isinstance(other, tuple):
raise NotImplementedError
if not (1 <= len(other) <= 4):
raise NotImplementedError
return astuple(self)[: len(other)], other
def __eq__(self, other):
try:
us, them = self._ensure_tuple(other)
except NotImplementedError:
return NotImplemented
return us == them
def __lt__(self, other):
try:
us, them = self._ensure_tuple(other)
except NotImplementedError:
return NotImplemented
# Since alphabetically "dev0" < "final" < "post1" < "post2", we don't
# have to do anything special with releaselevel for now.
return us < them
def __hash__(self):
return hash((self.year, self.minor, self.micro, self.releaselevel))

View File

@ -0,0 +1,9 @@
class VersionInfo:
@property
def year(self) -> int: ...
@property
def minor(self) -> int: ...
@property
def micro(self) -> int: ...
@property
def releaselevel(self) -> str: ...

View File

@ -0,0 +1,162 @@
# SPDX-License-Identifier: MIT
"""
Commonly useful converters.
"""
import typing
from ._compat import _AnnotationExtractor
from ._make import NOTHING, Converter, Factory, pipe
__all__ = [
"default_if_none",
"optional",
"pipe",
"to_bool",
]
def optional(converter):
"""
A converter that allows an attribute to be optional. An optional attribute
is one which can be set to `None`.
Type annotations will be inferred from the wrapped converter's, if it has
any.
Args:
converter (typing.Callable):
the converter that is used for non-`None` values.
.. versionadded:: 17.1.0
"""
if isinstance(converter, Converter):
def optional_converter(val, inst, field):
if val is None:
return None
return converter(val, inst, field)
else:
def optional_converter(val):
if val is None:
return None
return converter(val)
xtr = _AnnotationExtractor(converter)
t = xtr.get_first_param_type()
if t:
optional_converter.__annotations__["val"] = typing.Optional[t]
rt = xtr.get_return_type()
if rt:
optional_converter.__annotations__["return"] = typing.Optional[rt]
if isinstance(converter, Converter):
return Converter(optional_converter, takes_self=True, takes_field=True)
return optional_converter
def default_if_none(default=NOTHING, factory=None):
"""
A converter that allows to replace `None` values by *default* or the result
of *factory*.
Args:
default:
Value to be used if `None` is passed. Passing an instance of
`attrs.Factory` is supported, however the ``takes_self`` option is
*not*.
factory (typing.Callable):
A callable that takes no parameters whose result is used if `None`
is passed.
Raises:
TypeError: If **neither** *default* or *factory* is passed.
TypeError: If **both** *default* and *factory* are passed.
ValueError:
If an instance of `attrs.Factory` is passed with
``takes_self=True``.
.. versionadded:: 18.2.0
"""
if default is NOTHING and factory is None:
msg = "Must pass either `default` or `factory`."
raise TypeError(msg)
if default is not NOTHING and factory is not None:
msg = "Must pass either `default` or `factory` but not both."
raise TypeError(msg)
if factory is not None:
default = Factory(factory)
if isinstance(default, Factory):
if default.takes_self:
msg = "`takes_self` is not supported by default_if_none."
raise ValueError(msg)
def default_if_none_converter(val):
if val is not None:
return val
return default.factory()
else:
def default_if_none_converter(val):
if val is not None:
return val
return default
return default_if_none_converter
def to_bool(val):
"""
Convert "boolean" strings (for example, from environment variables) to real
booleans.
Values mapping to `True`:
- ``True``
- ``"true"`` / ``"t"``
- ``"yes"`` / ``"y"``
- ``"on"``
- ``"1"``
- ``1``
Values mapping to `False`:
- ``False``
- ``"false"`` / ``"f"``
- ``"no"`` / ``"n"``
- ``"off"``
- ``"0"``
- ``0``
Raises:
ValueError: For any other value.
.. versionadded:: 21.3.0
"""
if isinstance(val, str):
val = val.lower()
if val in (True, "true", "t", "yes", "y", "on", "1", 1):
return True
if val in (False, "false", "f", "no", "n", "off", "0", 0):
return False
msg = f"Cannot convert value to bool: {val!r}"
raise ValueError(msg)

View File

@ -0,0 +1,19 @@
from typing import Callable, Any, overload
from attrs import _ConverterType, _CallableConverterType
@overload
def pipe(*validators: _CallableConverterType) -> _CallableConverterType: ...
@overload
def pipe(*validators: _ConverterType) -> _ConverterType: ...
@overload
def optional(converter: _CallableConverterType) -> _CallableConverterType: ...
@overload
def optional(converter: _ConverterType) -> _ConverterType: ...
@overload
def default_if_none(default: Any) -> _CallableConverterType: ...
@overload
def default_if_none(
*, factory: Callable[[], Any]
) -> _CallableConverterType: ...
def to_bool(val: str | int | bool) -> bool: ...

View File

@ -0,0 +1,95 @@
# SPDX-License-Identifier: MIT
from __future__ import annotations
from typing import ClassVar
class FrozenError(AttributeError):
"""
A frozen/immutable instance or attribute have been attempted to be
modified.
It mirrors the behavior of ``namedtuples`` by using the same error message
and subclassing `AttributeError`.
.. versionadded:: 20.1.0
"""
msg = "can't set attribute"
args: ClassVar[tuple[str]] = [msg]
class FrozenInstanceError(FrozenError):
"""
A frozen instance has been attempted to be modified.
.. versionadded:: 16.1.0
"""
class FrozenAttributeError(FrozenError):
"""
A frozen attribute has been attempted to be modified.
.. versionadded:: 20.1.0
"""
class AttrsAttributeNotFoundError(ValueError):
"""
An *attrs* function couldn't find an attribute that the user asked for.
.. versionadded:: 16.2.0
"""
class NotAnAttrsClassError(ValueError):
"""
A non-*attrs* class has been passed into an *attrs* function.
.. versionadded:: 16.2.0
"""
class DefaultAlreadySetError(RuntimeError):
"""
A default has been set when defining the field and is attempted to be reset
using the decorator.
.. versionadded:: 17.1.0
"""
class UnannotatedAttributeError(RuntimeError):
"""
A class with ``auto_attribs=True`` has a field without a type annotation.
.. versionadded:: 17.3.0
"""
class PythonTooOldError(RuntimeError):
"""
It was attempted to use an *attrs* feature that requires a newer Python
version.
.. versionadded:: 18.2.0
"""
class NotCallableError(TypeError):
"""
A field requiring a callable has been set with a value that is not
callable.
.. versionadded:: 19.2.0
"""
def __init__(self, msg, value):
super(TypeError, self).__init__(msg, value)
self.msg = msg
self.value = value
def __str__(self):
return str(self.msg)

View File

@ -0,0 +1,17 @@
from typing import Any
class FrozenError(AttributeError):
msg: str = ...
class FrozenInstanceError(FrozenError): ...
class FrozenAttributeError(FrozenError): ...
class AttrsAttributeNotFoundError(ValueError): ...
class NotAnAttrsClassError(ValueError): ...
class DefaultAlreadySetError(RuntimeError): ...
class UnannotatedAttributeError(RuntimeError): ...
class PythonTooOldError(RuntimeError): ...
class NotCallableError(TypeError):
msg: str = ...
value: Any = ...
def __init__(self, msg: str, value: Any) -> None: ...

View File

@ -0,0 +1,72 @@
# SPDX-License-Identifier: MIT
"""
Commonly useful filters for `attrs.asdict` and `attrs.astuple`.
"""
from ._make import Attribute
def _split_what(what):
"""
Returns a tuple of `frozenset`s of classes and attributes.
"""
return (
frozenset(cls for cls in what if isinstance(cls, type)),
frozenset(cls for cls in what if isinstance(cls, str)),
frozenset(cls for cls in what if isinstance(cls, Attribute)),
)
def include(*what):
"""
Create a filter that only allows *what*.
Args:
what (list[type, str, attrs.Attribute]):
What to include. Can be a type, a name, or an attribute.
Returns:
Callable:
A callable that can be passed to `attrs.asdict`'s and
`attrs.astuple`'s *filter* argument.
.. versionchanged:: 23.1.0 Accept strings with field names.
"""
cls, names, attrs = _split_what(what)
def include_(attribute, value):
return (
value.__class__ in cls
or attribute.name in names
or attribute in attrs
)
return include_
def exclude(*what):
"""
Create a filter that does **not** allow *what*.
Args:
what (list[type, str, attrs.Attribute]):
What to exclude. Can be a type, a name, or an attribute.
Returns:
Callable:
A callable that can be passed to `attrs.asdict`'s and
`attrs.astuple`'s *filter* argument.
.. versionchanged:: 23.3.0 Accept field name string as input argument
"""
cls, names, attrs = _split_what(what)
def exclude_(attribute, value):
return not (
value.__class__ in cls
or attribute.name in names
or attribute in attrs
)
return exclude_

View File

@ -0,0 +1,6 @@
from typing import Any
from . import Attribute, _FilterType
def include(*what: type | str | Attribute[Any]) -> _FilterType[Any]: ...
def exclude(*what: type | str | Attribute[Any]) -> _FilterType[Any]: ...

View File

View File

@ -0,0 +1,79 @@
# SPDX-License-Identifier: MIT
"""
Commonly used hooks for on_setattr.
"""
from . import _config
from .exceptions import FrozenAttributeError
def pipe(*setters):
"""
Run all *setters* and return the return value of the last one.
.. versionadded:: 20.1.0
"""
def wrapped_pipe(instance, attrib, new_value):
rv = new_value
for setter in setters:
rv = setter(instance, attrib, rv)
return rv
return wrapped_pipe
def frozen(_, __, ___):
"""
Prevent an attribute to be modified.
.. versionadded:: 20.1.0
"""
raise FrozenAttributeError
def validate(instance, attrib, new_value):
"""
Run *attrib*'s validator on *new_value* if it has one.
.. versionadded:: 20.1.0
"""
if _config._run_validators is False:
return new_value
v = attrib.validator
if not v:
return new_value
v(instance, attrib, new_value)
return new_value
def convert(instance, attrib, new_value):
"""
Run *attrib*'s converter -- if it has one -- on *new_value* and return the
result.
.. versionadded:: 20.1.0
"""
c = attrib.converter
if c:
# This can be removed once we drop 3.8 and use attrs.Converter instead.
from ._make import Converter
if not isinstance(c, Converter):
return c(new_value)
return c(new_value, instance, attrib)
return new_value
# Sentinel for disabling class-wide *on_setattr* hooks for certain attributes.
# Sphinx's autodata stopped working, so the docstring is inlined in the API
# docs.
NO_OP = object()

View File

@ -0,0 +1,20 @@
from typing import Any, NewType, NoReturn, TypeVar
from . import Attribute
from attrs import _OnSetAttrType
_T = TypeVar("_T")
def frozen(
instance: Any, attribute: Attribute[Any], new_value: Any
) -> NoReturn: ...
def pipe(*setters: _OnSetAttrType) -> _OnSetAttrType: ...
def validate(instance: Any, attribute: Attribute[_T], new_value: _T) -> _T: ...
# convert is allowed to return Any, because they can be chained using pipe.
def convert(
instance: Any, attribute: Attribute[Any], new_value: Any
) -> Any: ...
_NoOpType = NewType("_NoOpType", object)
NO_OP: _NoOpType

View File

@ -0,0 +1,748 @@
# SPDX-License-Identifier: MIT
"""
Commonly useful validators.
"""
import operator
import re
from contextlib import contextmanager
from re import Pattern
from ._config import get_run_validators, set_run_validators
from ._make import _AndValidator, and_, attrib, attrs
from .converters import default_if_none
from .exceptions import NotCallableError
__all__ = [
"and_",
"deep_iterable",
"deep_mapping",
"disabled",
"ge",
"get_disabled",
"gt",
"in_",
"instance_of",
"is_callable",
"le",
"lt",
"matches_re",
"max_len",
"min_len",
"not_",
"optional",
"or_",
"set_disabled",
]
def set_disabled(disabled):
"""
Globally disable or enable running validators.
By default, they are run.
Args:
disabled (bool): If `True`, disable running all validators.
.. warning::
This function is not thread-safe!
.. versionadded:: 21.3.0
"""
set_run_validators(not disabled)
def get_disabled():
"""
Return a bool indicating whether validators are currently disabled or not.
Returns:
bool:`True` if validators are currently disabled.
.. versionadded:: 21.3.0
"""
return not get_run_validators()
@contextmanager
def disabled():
"""
Context manager that disables running validators within its context.
.. warning::
This context manager is not thread-safe!
.. versionadded:: 21.3.0
"""
set_run_validators(False)
try:
yield
finally:
set_run_validators(True)
@attrs(repr=False, slots=True, unsafe_hash=True)
class _InstanceOfValidator:
type = attrib()
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if not isinstance(value, self.type):
msg = f"'{attr.name}' must be {self.type!r} (got {value!r} that is a {value.__class__!r})."
raise TypeError(
msg,
attr,
self.type,
value,
)
def __repr__(self):
return f"<instance_of validator for type {self.type!r}>"
def instance_of(type):
"""
A validator that raises a `TypeError` if the initializer is called with a
wrong type for this particular attribute (checks are performed using
`isinstance` therefore it's also valid to pass a tuple of types).
Args:
type (type | tuple[type]): The type to check for.
Raises:
TypeError:
With a human readable error message, the attribute (of type
`attrs.Attribute`), the expected type, and the value it got.
"""
return _InstanceOfValidator(type)
@attrs(repr=False, frozen=True, slots=True)
class _MatchesReValidator:
pattern = attrib()
match_func = attrib()
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if not self.match_func(value):
msg = f"'{attr.name}' must match regex {self.pattern.pattern!r} ({value!r} doesn't)"
raise ValueError(
msg,
attr,
self.pattern,
value,
)
def __repr__(self):
return f"<matches_re validator for pattern {self.pattern!r}>"
def matches_re(regex, flags=0, func=None):
r"""
A validator that raises `ValueError` if the initializer is called with a
string that doesn't match *regex*.
Args:
regex (str, re.Pattern):
A regex string or precompiled pattern to match against
flags (int):
Flags that will be passed to the underlying re function (default 0)
func (typing.Callable):
Which underlying `re` function to call. Valid options are
`re.fullmatch`, `re.search`, and `re.match`; the default `None`
means `re.fullmatch`. For performance reasons, the pattern is
always precompiled using `re.compile`.
.. versionadded:: 19.2.0
.. versionchanged:: 21.3.0 *regex* can be a pre-compiled pattern.
"""
valid_funcs = (re.fullmatch, None, re.search, re.match)
if func not in valid_funcs:
msg = "'func' must be one of {}.".format(
", ".join(
sorted((e and e.__name__) or "None" for e in set(valid_funcs))
)
)
raise ValueError(msg)
if isinstance(regex, Pattern):
if flags:
msg = "'flags' can only be used with a string pattern; pass flags to re.compile() instead"
raise TypeError(msg)
pattern = regex
else:
pattern = re.compile(regex, flags)
if func is re.match:
match_func = pattern.match
elif func is re.search:
match_func = pattern.search
else:
match_func = pattern.fullmatch
return _MatchesReValidator(pattern, match_func)
@attrs(repr=False, slots=True, unsafe_hash=True)
class _OptionalValidator:
validator = attrib()
def __call__(self, inst, attr, value):
if value is None:
return
self.validator(inst, attr, value)
def __repr__(self):
return f"<optional validator for {self.validator!r} or None>"
def optional(validator):
"""
A validator that makes an attribute optional. An optional attribute is one
which can be set to `None` in addition to satisfying the requirements of
the sub-validator.
Args:
validator
(typing.Callable | tuple[typing.Callable] | list[typing.Callable]):
A validator (or validators) that is used for non-`None` values.
.. versionadded:: 15.1.0
.. versionchanged:: 17.1.0 *validator* can be a list of validators.
.. versionchanged:: 23.1.0 *validator* can also be a tuple of validators.
"""
if isinstance(validator, (list, tuple)):
return _OptionalValidator(_AndValidator(validator))
return _OptionalValidator(validator)
@attrs(repr=False, slots=True, unsafe_hash=True)
class _InValidator:
options = attrib()
_original_options = attrib(hash=False)
def __call__(self, inst, attr, value):
try:
in_options = value in self.options
except TypeError: # e.g. `1 in "abc"`
in_options = False
if not in_options:
msg = f"'{attr.name}' must be in {self._original_options!r} (got {value!r})"
raise ValueError(
msg,
attr,
self._original_options,
value,
)
def __repr__(self):
return f"<in_ validator with options {self._original_options!r}>"
def in_(options):
"""
A validator that raises a `ValueError` if the initializer is called with a
value that does not belong in the *options* provided.
The check is performed using ``value in options``, so *options* has to
support that operation.
To keep the validator hashable, dicts, lists, and sets are transparently
transformed into a `tuple`.
Args:
options: Allowed options.
Raises:
ValueError:
With a human readable error message, the attribute (of type
`attrs.Attribute`), the expected options, and the value it got.
.. versionadded:: 17.1.0
.. versionchanged:: 22.1.0
The ValueError was incomplete until now and only contained the human
readable error message. Now it contains all the information that has
been promised since 17.1.0.
.. versionchanged:: 24.1.0
*options* that are a list, dict, or a set are now transformed into a
tuple to keep the validator hashable.
"""
repr_options = options
if isinstance(options, (list, dict, set)):
options = tuple(options)
return _InValidator(options, repr_options)
@attrs(repr=False, slots=False, unsafe_hash=True)
class _IsCallableValidator:
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if not callable(value):
message = (
"'{name}' must be callable "
"(got {value!r} that is a {actual!r})."
)
raise NotCallableError(
msg=message.format(
name=attr.name, value=value, actual=value.__class__
),
value=value,
)
def __repr__(self):
return "<is_callable validator>"
def is_callable():
"""
A validator that raises a `attrs.exceptions.NotCallableError` if the
initializer is called with a value for this particular attribute that is
not callable.
.. versionadded:: 19.1.0
Raises:
attrs.exceptions.NotCallableError:
With a human readable error message containing the attribute
(`attrs.Attribute`) name, and the value it got.
"""
return _IsCallableValidator()
@attrs(repr=False, slots=True, unsafe_hash=True)
class _DeepIterable:
member_validator = attrib(validator=is_callable())
iterable_validator = attrib(
default=None, validator=optional(is_callable())
)
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if self.iterable_validator is not None:
self.iterable_validator(inst, attr, value)
for member in value:
self.member_validator(inst, attr, member)
def __repr__(self):
iterable_identifier = (
""
if self.iterable_validator is None
else f" {self.iterable_validator!r}"
)
return (
f"<deep_iterable validator for{iterable_identifier}"
f" iterables of {self.member_validator!r}>"
)
def deep_iterable(member_validator, iterable_validator=None):
"""
A validator that performs deep validation of an iterable.
Args:
member_validator: Validator(s) to apply to iterable members.
iterable_validator:
Validator(s) to apply to iterable itself (optional).
Raises
TypeError: if any sub-validators fail
.. versionadded:: 19.1.0
.. versionchanged:: 25.4.0
*member_validator* and *iterable_validator* can now be a list or tuple
of validators.
"""
if isinstance(member_validator, (list, tuple)):
member_validator = and_(*member_validator)
if isinstance(iterable_validator, (list, tuple)):
iterable_validator = and_(*iterable_validator)
return _DeepIterable(member_validator, iterable_validator)
@attrs(repr=False, slots=True, unsafe_hash=True)
class _DeepMapping:
key_validator = attrib(validator=optional(is_callable()))
value_validator = attrib(validator=optional(is_callable()))
mapping_validator = attrib(validator=optional(is_callable()))
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if self.mapping_validator is not None:
self.mapping_validator(inst, attr, value)
for key in value:
if self.key_validator is not None:
self.key_validator(inst, attr, key)
if self.value_validator is not None:
self.value_validator(inst, attr, value[key])
def __repr__(self):
return f"<deep_mapping validator for objects mapping {self.key_validator!r} to {self.value_validator!r}>"
def deep_mapping(
key_validator=None, value_validator=None, mapping_validator=None
):
"""
A validator that performs deep validation of a dictionary.
All validators are optional, but at least one of *key_validator* or
*value_validator* must be provided.
Args:
key_validator: Validator(s) to apply to dictionary keys.
value_validator: Validator(s) to apply to dictionary values.
mapping_validator:
Validator(s) to apply to top-level mapping attribute.
.. versionadded:: 19.1.0
.. versionchanged:: 25.4.0
*key_validator* and *value_validator* are now optional, but at least one
of them must be provided.
.. versionchanged:: 25.4.0
*key_validator*, *value_validator*, and *mapping_validator* can now be a
list or tuple of validators.
Raises:
TypeError: If any sub-validator fails on validation.
ValueError:
If neither *key_validator* nor *value_validator* is provided on
instantiation.
"""
if key_validator is None and value_validator is None:
msg = (
"At least one of key_validator or value_validator must be provided"
)
raise ValueError(msg)
if isinstance(key_validator, (list, tuple)):
key_validator = and_(*key_validator)
if isinstance(value_validator, (list, tuple)):
value_validator = and_(*value_validator)
if isinstance(mapping_validator, (list, tuple)):
mapping_validator = and_(*mapping_validator)
return _DeepMapping(key_validator, value_validator, mapping_validator)
@attrs(repr=False, frozen=True, slots=True)
class _NumberValidator:
bound = attrib()
compare_op = attrib()
compare_func = attrib()
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if not self.compare_func(value, self.bound):
msg = f"'{attr.name}' must be {self.compare_op} {self.bound}: {value}"
raise ValueError(msg)
def __repr__(self):
return f"<Validator for x {self.compare_op} {self.bound}>"
def lt(val):
"""
A validator that raises `ValueError` if the initializer is called with a
number larger or equal to *val*.
The validator uses `operator.lt` to compare the values.
Args:
val: Exclusive upper bound for values.
.. versionadded:: 21.3.0
"""
return _NumberValidator(val, "<", operator.lt)
def le(val):
"""
A validator that raises `ValueError` if the initializer is called with a
number greater than *val*.
The validator uses `operator.le` to compare the values.
Args:
val: Inclusive upper bound for values.
.. versionadded:: 21.3.0
"""
return _NumberValidator(val, "<=", operator.le)
def ge(val):
"""
A validator that raises `ValueError` if the initializer is called with a
number smaller than *val*.
The validator uses `operator.ge` to compare the values.
Args:
val: Inclusive lower bound for values
.. versionadded:: 21.3.0
"""
return _NumberValidator(val, ">=", operator.ge)
def gt(val):
"""
A validator that raises `ValueError` if the initializer is called with a
number smaller or equal to *val*.
The validator uses `operator.gt` to compare the values.
Args:
val: Exclusive lower bound for values
.. versionadded:: 21.3.0
"""
return _NumberValidator(val, ">", operator.gt)
@attrs(repr=False, frozen=True, slots=True)
class _MaxLengthValidator:
max_length = attrib()
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if len(value) > self.max_length:
msg = f"Length of '{attr.name}' must be <= {self.max_length}: {len(value)}"
raise ValueError(msg)
def __repr__(self):
return f"<max_len validator for {self.max_length}>"
def max_len(length):
"""
A validator that raises `ValueError` if the initializer is called
with a string or iterable that is longer than *length*.
Args:
length (int): Maximum length of the string or iterable
.. versionadded:: 21.3.0
"""
return _MaxLengthValidator(length)
@attrs(repr=False, frozen=True, slots=True)
class _MinLengthValidator:
min_length = attrib()
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if len(value) < self.min_length:
msg = f"Length of '{attr.name}' must be >= {self.min_length}: {len(value)}"
raise ValueError(msg)
def __repr__(self):
return f"<min_len validator for {self.min_length}>"
def min_len(length):
"""
A validator that raises `ValueError` if the initializer is called
with a string or iterable that is shorter than *length*.
Args:
length (int): Minimum length of the string or iterable
.. versionadded:: 22.1.0
"""
return _MinLengthValidator(length)
@attrs(repr=False, slots=True, unsafe_hash=True)
class _SubclassOfValidator:
type = attrib()
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if not issubclass(value, self.type):
msg = f"'{attr.name}' must be a subclass of {self.type!r} (got {value!r})."
raise TypeError(
msg,
attr,
self.type,
value,
)
def __repr__(self):
return f"<subclass_of validator for type {self.type!r}>"
def _subclass_of(type):
"""
A validator that raises a `TypeError` if the initializer is called with a
wrong type for this particular attribute (checks are performed using
`issubclass` therefore it's also valid to pass a tuple of types).
Args:
type (type | tuple[type, ...]): The type(s) to check for.
Raises:
TypeError:
With a human readable error message, the attribute (of type
`attrs.Attribute`), the expected type, and the value it got.
"""
return _SubclassOfValidator(type)
@attrs(repr=False, slots=True, unsafe_hash=True)
class _NotValidator:
validator = attrib()
msg = attrib(
converter=default_if_none(
"not_ validator child '{validator!r}' "
"did not raise a captured error"
)
)
exc_types = attrib(
validator=deep_iterable(
member_validator=_subclass_of(Exception),
iterable_validator=instance_of(tuple),
),
)
def __call__(self, inst, attr, value):
try:
self.validator(inst, attr, value)
except self.exc_types:
pass # suppress error to invert validity
else:
raise ValueError(
self.msg.format(
validator=self.validator,
exc_types=self.exc_types,
),
attr,
self.validator,
value,
self.exc_types,
)
def __repr__(self):
return f"<not_ validator wrapping {self.validator!r}, capturing {self.exc_types!r}>"
def not_(validator, *, msg=None, exc_types=(ValueError, TypeError)):
"""
A validator that wraps and logically 'inverts' the validator passed to it.
It will raise a `ValueError` if the provided validator *doesn't* raise a
`ValueError` or `TypeError` (by default), and will suppress the exception
if the provided validator *does*.
Intended to be used with existing validators to compose logic without
needing to create inverted variants, for example, ``not_(in_(...))``.
Args:
validator: A validator to be logically inverted.
msg (str):
Message to raise if validator fails. Formatted with keys
``exc_types`` and ``validator``.
exc_types (tuple[type, ...]):
Exception type(s) to capture. Other types raised by child
validators will not be intercepted and pass through.
Raises:
ValueError:
With a human readable error message, the attribute (of type
`attrs.Attribute`), the validator that failed to raise an
exception, the value it got, and the expected exception types.
.. versionadded:: 22.2.0
"""
try:
exc_types = tuple(exc_types)
except TypeError:
exc_types = (exc_types,)
return _NotValidator(validator, msg, exc_types)
@attrs(repr=False, slots=True, unsafe_hash=True)
class _OrValidator:
validators = attrib()
def __call__(self, inst, attr, value):
for v in self.validators:
try:
v(inst, attr, value)
except Exception: # noqa: BLE001, PERF203, S112
continue
else:
return
msg = f"None of {self.validators!r} satisfied for value {value!r}"
raise ValueError(msg)
def __repr__(self):
return f"<or validator wrapping {self.validators!r}>"
def or_(*validators):
"""
A validator that composes multiple validators into one.
When called on a value, it runs all wrapped validators until one of them is
satisfied.
Args:
validators (~collections.abc.Iterable[typing.Callable]):
Arbitrary number of validators.
Raises:
ValueError:
If no validator is satisfied. Raised with a human-readable error
message listing all the wrapped validators and the value that
failed all of them.
.. versionadded:: 24.1.0
"""
vals = []
for v in validators:
vals.extend(v.validators if isinstance(v, _OrValidator) else [v])
return _OrValidator(tuple(vals))

View File

@ -0,0 +1,140 @@
from types import UnionType
from typing import (
Any,
AnyStr,
Callable,
Container,
ContextManager,
Iterable,
Mapping,
Match,
Pattern,
TypeVar,
overload,
)
from attrs import _ValidatorType
from attrs import _ValidatorArgType
_T = TypeVar("_T")
_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")
_T3 = TypeVar("_T3")
_T4 = TypeVar("_T4")
_T5 = TypeVar("_T5")
_T6 = TypeVar("_T6")
_I = TypeVar("_I", bound=Iterable)
_K = TypeVar("_K")
_V = TypeVar("_V")
_M = TypeVar("_M", bound=Mapping)
def set_disabled(run: bool) -> None: ...
def get_disabled() -> bool: ...
def disabled() -> ContextManager[None]: ...
# To be more precise on instance_of use some overloads.
# If there are more than 3 items in the tuple then we fall back to Any
@overload
def instance_of(type: type[_T]) -> _ValidatorType[_T]: ...
@overload
def instance_of(type: tuple[type[_T]]) -> _ValidatorType[_T]: ...
@overload
def instance_of(
type: tuple[type[_T1], type[_T2]],
) -> _ValidatorType[_T1 | _T2]: ...
@overload
def instance_of(
type: tuple[type[_T1], type[_T2], type[_T3]],
) -> _ValidatorType[_T1 | _T2 | _T3]: ...
@overload
def instance_of(type: tuple[type, ...]) -> _ValidatorType[Any]: ...
@overload
def instance_of(type: UnionType) -> _ValidatorType[Any]: ...
def optional(
validator: (
_ValidatorType[_T]
| list[_ValidatorType[_T]]
| tuple[_ValidatorType[_T]]
),
) -> _ValidatorType[_T | None]: ...
def in_(options: Container[_T]) -> _ValidatorType[_T]: ...
def and_(*validators: _ValidatorType[_T]) -> _ValidatorType[_T]: ...
def matches_re(
regex: Pattern[AnyStr] | AnyStr,
flags: int = ...,
func: Callable[[AnyStr, AnyStr, int], Match[AnyStr] | None] | None = ...,
) -> _ValidatorType[AnyStr]: ...
def deep_iterable(
member_validator: _ValidatorArgType[_T],
iterable_validator: _ValidatorArgType[_I] | None = ...,
) -> _ValidatorType[_I]: ...
@overload
def deep_mapping(
key_validator: _ValidatorArgType[_K],
value_validator: _ValidatorArgType[_V] | None = ...,
mapping_validator: _ValidatorArgType[_M] | None = ...,
) -> _ValidatorType[_M]: ...
@overload
def deep_mapping(
key_validator: _ValidatorArgType[_K] | None = ...,
value_validator: _ValidatorArgType[_V] = ...,
mapping_validator: _ValidatorArgType[_M] | None = ...,
) -> _ValidatorType[_M]: ...
def is_callable() -> _ValidatorType[_T]: ...
def lt(val: _T) -> _ValidatorType[_T]: ...
def le(val: _T) -> _ValidatorType[_T]: ...
def ge(val: _T) -> _ValidatorType[_T]: ...
def gt(val: _T) -> _ValidatorType[_T]: ...
def max_len(length: int) -> _ValidatorType[_T]: ...
def min_len(length: int) -> _ValidatorType[_T]: ...
def not_(
validator: _ValidatorType[_T],
*,
msg: str | None = None,
exc_types: type[Exception] | Iterable[type[Exception]] = ...,
) -> _ValidatorType[_T]: ...
@overload
def or_(
__v1: _ValidatorType[_T1],
__v2: _ValidatorType[_T2],
) -> _ValidatorType[_T1 | _T2]: ...
@overload
def or_(
__v1: _ValidatorType[_T1],
__v2: _ValidatorType[_T2],
__v3: _ValidatorType[_T3],
) -> _ValidatorType[_T1 | _T2 | _T3]: ...
@overload
def or_(
__v1: _ValidatorType[_T1],
__v2: _ValidatorType[_T2],
__v3: _ValidatorType[_T3],
__v4: _ValidatorType[_T4],
) -> _ValidatorType[_T1 | _T2 | _T3 | _T4]: ...
@overload
def or_(
__v1: _ValidatorType[_T1],
__v2: _ValidatorType[_T2],
__v3: _ValidatorType[_T3],
__v4: _ValidatorType[_T4],
__v5: _ValidatorType[_T5],
) -> _ValidatorType[_T1 | _T2 | _T3 | _T4 | _T5]: ...
@overload
def or_(
__v1: _ValidatorType[_T1],
__v2: _ValidatorType[_T2],
__v3: _ValidatorType[_T3],
__v4: _ValidatorType[_T4],
__v5: _ValidatorType[_T5],
__v6: _ValidatorType[_T6],
) -> _ValidatorType[_T1 | _T2 | _T3 | _T4 | _T5 | _T6]: ...
@overload
def or_(
__v1: _ValidatorType[Any],
__v2: _ValidatorType[Any],
__v3: _ValidatorType[Any],
__v4: _ValidatorType[Any],
__v5: _ValidatorType[Any],
__v6: _ValidatorType[Any],
*validators: _ValidatorType[Any],
) -> _ValidatorType[Any]: ...

View File

@ -0,0 +1 @@
pip

View File

@ -0,0 +1,235 @@
Metadata-Version: 2.4
Name: attrs
Version: 25.4.0
Summary: Classes Without Boilerplate
Project-URL: Documentation, https://www.attrs.org/
Project-URL: Changelog, https://www.attrs.org/en/stable/changelog.html
Project-URL: GitHub, https://github.com/python-attrs/attrs
Project-URL: Funding, https://github.com/sponsors/hynek
Project-URL: Tidelift, https://tidelift.com/subscription/pkg/pypi-attrs?utm_source=pypi-attrs&utm_medium=pypi
Author-email: Hynek Schlawack <hs@ox.cx>
License-Expression: MIT
License-File: LICENSE
Keywords: attribute,boilerplate,class
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
<p align="center">
<a href="https://www.attrs.org/">
<img src="https://raw.githubusercontent.com/python-attrs/attrs/main/docs/_static/attrs_logo.svg" width="35%" alt="attrs" />
</a>
</p>
*attrs* is the Python package that will bring back the **joy** of **writing classes** by relieving you from the drudgery of implementing object protocols (aka [dunder methods](https://www.attrs.org/en/latest/glossary.html#term-dunder-methods)).
Trusted by NASA for [Mars missions since 2020](https://github.com/readme/featured/nasa-ingenuity-helicopter)!
Its main goal is to help you to write **concise** and **correct** software without slowing down your code.
## Sponsors
*attrs* would not be possible without our [amazing sponsors](https://github.com/sponsors/hynek).
Especially those generously supporting us at the *The Organization* tier and higher:
<!-- sponsor-break-begin -->
<p align="center">
<!-- [[[cog
import pathlib, tomllib
for sponsor in tomllib.loads(pathlib.Path("pyproject.toml").read_text())["tool"]["sponcon"]["sponsors"]:
print(f'<a href="{sponsor["url"]}"><img title="{sponsor["title"]}" src="https://www.attrs.org/en/25.4.0/_static/sponsors/{sponsor["img"]}" width="190" /></a>')
]]] -->
<a href="https://www.variomedia.de/"><img title="Variomedia AG" src="https://www.attrs.org/en/25.4.0/_static/sponsors/Variomedia.svg" width="190" /></a>
<a href="https://tidelift.com/?utm_source=lifter&utm_medium=referral&utm_campaign=hynek"><img title="Tidelift" src="https://www.attrs.org/en/25.4.0/_static/sponsors/Tidelift.svg" width="190" /></a>
<a href="https://privacy-solutions.org/"><img title="Privacy Solutions" src="https://www.attrs.org/en/25.4.0/_static/sponsors/Privacy-Solutions.svg" width="190" /></a>
<a href="https://filepreviews.io/"><img title="FilePreviews" src="https://www.attrs.org/en/25.4.0/_static/sponsors/FilePreviews.svg" width="190" /></a>
<a href="https://polar.sh/"><img title="Polar" src="https://www.attrs.org/en/25.4.0/_static/sponsors/Polar.svg" width="190" /></a>
<!-- [[[end]]] -->
</p>
<!-- sponsor-break-end -->
<p align="center">
<strong>Please consider <a href="https://github.com/sponsors/hynek">joining them</a> to help make <em>attrs</em>s maintenance more sustainable!</strong>
</p>
<!-- teaser-end -->
## Example
*attrs* gives you a class decorator and a way to declaratively define the attributes on that class:
<!-- code-begin -->
```pycon
>>> from attrs import asdict, define, make_class, Factory
>>> @define
... class SomeClass:
... a_number: int = 42
... list_of_numbers: list[int] = Factory(list)
...
... def hard_math(self, another_number):
... return self.a_number + sum(self.list_of_numbers) * another_number
>>> sc = SomeClass(1, [1, 2, 3])
>>> sc
SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
>>> sc.hard_math(3)
19
>>> sc == SomeClass(1, [1, 2, 3])
True
>>> sc != SomeClass(2, [3, 2, 1])
True
>>> asdict(sc)
{'a_number': 1, 'list_of_numbers': [1, 2, 3]}
>>> SomeClass()
SomeClass(a_number=42, list_of_numbers=[])
>>> C = make_class("C", ["a", "b"])
>>> C("foo", "bar")
C(a='foo', b='bar')
```
After *declaring* your attributes, *attrs* gives you:
- a concise and explicit overview of the class's attributes,
- a nice human-readable `__repr__`,
- equality-checking methods,
- an initializer,
- and much more,
*without* writing dull boilerplate code again and again and *without* runtime performance penalties.
---
This example uses *attrs*'s modern APIs that have been introduced in version 20.1.0, and the *attrs* package import name that has been added in version 21.3.0.
The classic APIs (`@attr.s`, `attr.ib`, plus their serious-business aliases) and the `attr` package import name will remain **indefinitely**.
Check out [*On The Core API Names*](https://www.attrs.org/en/latest/names.html) for an in-depth explanation!
### Hate Type Annotations!?
No problem!
Types are entirely **optional** with *attrs*.
Simply assign `attrs.field()` to the attributes instead of annotating them with types:
```python
from attrs import define, field
@define
class SomeClass:
a_number = field(default=42)
list_of_numbers = field(factory=list)
```
## Data Classes
On the tin, *attrs* might remind you of `dataclasses` (and indeed, `dataclasses` [are a descendant](https://hynek.me/articles/import-attrs/) of *attrs*).
In practice it does a lot more and is more flexible.
For instance, it allows you to define [special handling of NumPy arrays for equality checks](https://www.attrs.org/en/stable/comparison.html#customization), allows more ways to [plug into the initialization process](https://www.attrs.org/en/stable/init.html#hooking-yourself-into-initialization), has a replacement for `__init_subclass__`, and allows for stepping through the generated methods using a debugger.
For more details, please refer to our [comparison page](https://www.attrs.org/en/stable/why.html#data-classes), but generally speaking, we are more likely to commit crimes against nature to make things work that one would expect to work, but that are quite complicated in practice.
## Project Information
- [**Changelog**](https://www.attrs.org/en/stable/changelog.html)
- [**Documentation**](https://www.attrs.org/)
- [**PyPI**](https://pypi.org/project/attrs/)
- [**Source Code**](https://github.com/python-attrs/attrs)
- [**Contributing**](https://github.com/python-attrs/attrs/blob/main/.github/CONTRIBUTING.md)
- [**Third-party Extensions**](https://github.com/python-attrs/attrs/wiki/Extensions-to-attrs)
- **Get Help**: use the `python-attrs` tag on [Stack Overflow](https://stackoverflow.com/questions/tagged/python-attrs)
### *attrs* for Enterprise
Available as part of the [Tidelift Subscription](https://tidelift.com/?utm_source=lifter&utm_medium=referral&utm_campaign=hynek).
The maintainers of *attrs* and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications.
Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use.
## Release Information
### Backwards-incompatible Changes
- Class-level `kw_only=True` behavior is now consistent with `dataclasses`.
Previously, a class that sets `kw_only=True` makes all attributes keyword-only, including those from base classes.
If an attribute sets `kw_only=False`, that setting is ignored, and it is still made keyword-only.
Now, only the attributes defined in that class that doesn't explicitly set `kw_only=False` are made keyword-only.
This shouldn't be a problem for most users, unless you have a pattern like this:
```python
@attrs.define(kw_only=True)
class Base:
a: int
b: int = attrs.field(default=1, kw_only=False)
@attrs.define
class Subclass(Base):
c: int
```
Here, we have a `kw_only=True` *attrs* class (`Base`) with an attribute that sets `kw_only=False` and has a default (`Base.b`), and then create a subclass (`Subclass`) with required arguments (`Subclass.c`).
Previously this would work, since it would make `Base.b` keyword-only, but now this fails since `Base.b` is positional, and we have a required positional argument (`Subclass.c`) following another argument with defaults.
[#1457](https://github.com/python-attrs/attrs/issues/1457)
### Changes
- Values passed to the `__init__()` method of `attrs` classes are now correctly passed to `__attrs_pre_init__()` instead of their default values (in cases where *kw_only* was not specified).
[#1427](https://github.com/python-attrs/attrs/issues/1427)
- Added support for Python 3.14 and [PEP 749](https://peps.python.org/pep-0749/).
[#1446](https://github.com/python-attrs/attrs/issues/1446),
[#1451](https://github.com/python-attrs/attrs/issues/1451)
- `attrs.validators.deep_mapping()` now allows to leave out either *key_validator* xor *value_validator*.
[#1448](https://github.com/python-attrs/attrs/issues/1448)
- `attrs.validators.deep_iterator()` and `attrs.validators.deep_mapping()` now accept lists and tuples for all validators and wrap them into a `attrs.validators.and_()`.
[#1449](https://github.com/python-attrs/attrs/issues/1449)
- Added a new **experimental** way to inspect classes:
`attrs.inspect(cls)` returns the _effective_ class-wide parameters that were used by *attrs* to construct the class.
The returned class is the same data structure that *attrs* uses internally to decide how to construct the final class.
[#1454](https://github.com/python-attrs/attrs/issues/1454)
- Fixed annotations for `attrs.field(converter=...)`.
Previously, a `tuple` of converters was only accepted if it had exactly one element.
[#1461](https://github.com/python-attrs/attrs/issues/1461)
- The performance of `attrs.asdict()` has been improved by 45260%.
[#1463](https://github.com/python-attrs/attrs/issues/1463)
- The performance of `attrs.astuple()` has been improved by 49270%.
[#1469](https://github.com/python-attrs/attrs/issues/1469)
- The type annotation for `attrs.validators.or_()` now allows for different types of validators.
This was only an issue on Pyright.
[#1474](https://github.com/python-attrs/attrs/issues/1474)
---
[Full changelog →](https://www.attrs.org/en/stable/changelog.html)

View File

@ -0,0 +1,55 @@
attr/__init__.py,sha256=fOYIvt1eGSqQre4uCS3sJWKZ0mwAuC8UD6qba5OS9_U,2057
attr/__init__.pyi,sha256=IZkzIjvtbRqDWGkDBIF9dd12FgDa379JYq3GHnVOvFQ,11309
attr/__pycache__/__init__.cpython-312.pyc,,
attr/__pycache__/_cmp.cpython-312.pyc,,
attr/__pycache__/_compat.cpython-312.pyc,,
attr/__pycache__/_config.cpython-312.pyc,,
attr/__pycache__/_funcs.cpython-312.pyc,,
attr/__pycache__/_make.cpython-312.pyc,,
attr/__pycache__/_next_gen.cpython-312.pyc,,
attr/__pycache__/_version_info.cpython-312.pyc,,
attr/__pycache__/converters.cpython-312.pyc,,
attr/__pycache__/exceptions.cpython-312.pyc,,
attr/__pycache__/filters.cpython-312.pyc,,
attr/__pycache__/setters.cpython-312.pyc,,
attr/__pycache__/validators.cpython-312.pyc,,
attr/_cmp.py,sha256=3Nn1TjxllUYiX_nJoVnEkXoDk0hM1DYKj5DE7GZe4i0,4117
attr/_cmp.pyi,sha256=U-_RU_UZOyPUEQzXE6RMYQQcjkZRY25wTH99sN0s7MM,368
attr/_compat.py,sha256=x0g7iEUOnBVJC72zyFCgb1eKqyxS-7f2LGnNyZ_r95s,2829
attr/_config.py,sha256=dGq3xR6fgZEF6UBt_L0T-eUHIB4i43kRmH0P28sJVw8,843
attr/_funcs.py,sha256=Ix5IETTfz5F01F-12MF_CSFomIn2h8b67EVVz2gCtBE,16479
attr/_make.py,sha256=NRJDGS8syg2h3YNflVNoK2FwR3CpdSZxx8M6lacwljA,104141
attr/_next_gen.py,sha256=BQtCUlzwg2gWHTYXBQvrEYBnzBUrDvO57u0Py6UCPhc,26274
attr/_typing_compat.pyi,sha256=XDP54TUn-ZKhD62TOQebmzrwFyomhUCoGRpclb6alRA,469
attr/_version_info.py,sha256=w4R-FYC3NK_kMkGUWJlYP4cVAlH9HRaC-um3fcjYkHM,2222
attr/_version_info.pyi,sha256=x_M3L3WuB7r_ULXAWjx959udKQ4HLB8l-hsc1FDGNvk,209
attr/converters.py,sha256=GlDeOzPeTFgeBBLbj9G57Ez5lAk68uhSALRYJ_exe84,3861
attr/converters.pyi,sha256=orU2bff-VjQa2kMDyvnMQV73oJT2WRyQuw4ZR1ym1bE,643
attr/exceptions.py,sha256=HRFq4iybmv7-DcZwyjl6M1euM2YeJVK_hFxuaBGAngI,1977
attr/exceptions.pyi,sha256=zZq8bCUnKAy9mDtBEw42ZhPhAUIHoTKedDQInJD883M,539
attr/filters.py,sha256=ZBiKWLp3R0LfCZsq7X11pn9WX8NslS2wXM4jsnLOGc8,1795
attr/filters.pyi,sha256=3J5BG-dTxltBk1_-RuNRUHrv2qu1v8v4aDNAQ7_mifA,208
attr/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
attr/setters.py,sha256=5-dcT63GQK35ONEzSgfXCkbB7pPkaR-qv15mm4PVSzQ,1617
attr/setters.pyi,sha256=NnVkaFU1BB4JB8E4JuXyrzTUgvtMpj8p3wBdJY7uix4,584
attr/validators.py,sha256=1BnYGTuYvSucGEI4ju-RPNJteVzG0ZlfWpJiWoSFHQ8,21458
attr/validators.pyi,sha256=ftmW3m4KJ3pQcIXAj-BejT7BY4ZfqrC1G-5W7XvoPds,4082
attrs-25.4.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
attrs-25.4.0.dist-info/METADATA,sha256=2Rerxj7agcMRxiwdkt6lC2guqHAmkGKCH13nWWK7ZoQ,10473
attrs-25.4.0.dist-info/RECORD,,
attrs-25.4.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
attrs-25.4.0.dist-info/licenses/LICENSE,sha256=iCEVyV38KvHutnFPjsbVy8q_Znyv-HKfQkINpj9xTp8,1109
attrs/__init__.py,sha256=RxaAZNwYiEh-fcvHLZNpQ_DWKni73M_jxEPEftiq1Zc,1183
attrs/__init__.pyi,sha256=2gV79g9UxJppGSM48hAZJ6h_MHb70dZoJL31ZNJeZYI,9416
attrs/__pycache__/__init__.cpython-312.pyc,,
attrs/__pycache__/converters.cpython-312.pyc,,
attrs/__pycache__/exceptions.cpython-312.pyc,,
attrs/__pycache__/filters.cpython-312.pyc,,
attrs/__pycache__/setters.cpython-312.pyc,,
attrs/__pycache__/validators.cpython-312.pyc,,
attrs/converters.py,sha256=8kQljrVwfSTRu8INwEk8SI0eGrzmWftsT7rM0EqyohM,76
attrs/exceptions.py,sha256=ACCCmg19-vDFaDPY9vFl199SPXCQMN_bENs4DALjzms,76
attrs/filters.py,sha256=VOUMZug9uEU6dUuA0dF1jInUK0PL3fLgP0VBS5d-CDE,73
attrs/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
attrs/setters.py,sha256=eL1YidYQV3T2h9_SYIZSZR1FAcHGb1TuCTy0E0Lv2SU,73
attrs/validators.py,sha256=xcy6wD5TtTkdCG1f4XWbocPSO0faBjk5IfVJfP6SUj0,76

View File

@ -0,0 +1,4 @@
Wheel-Version: 1.0
Generator: hatchling 1.27.0
Root-Is-Purelib: true
Tag: py3-none-any

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 Hynek Schlawack and the attrs contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,72 @@
# SPDX-License-Identifier: MIT
from attr import (
NOTHING,
Attribute,
AttrsInstance,
Converter,
Factory,
NothingType,
_make_getattr,
assoc,
cmp_using,
define,
evolve,
field,
fields,
fields_dict,
frozen,
has,
make_class,
mutable,
resolve_types,
validate,
)
from attr._make import ClassProps
from attr._next_gen import asdict, astuple, inspect
from . import converters, exceptions, filters, setters, validators
__all__ = [
"NOTHING",
"Attribute",
"AttrsInstance",
"ClassProps",
"Converter",
"Factory",
"NothingType",
"__author__",
"__copyright__",
"__description__",
"__doc__",
"__email__",
"__license__",
"__title__",
"__url__",
"__version__",
"__version_info__",
"asdict",
"assoc",
"astuple",
"cmp_using",
"converters",
"define",
"evolve",
"exceptions",
"field",
"fields",
"fields_dict",
"filters",
"frozen",
"has",
"inspect",
"make_class",
"mutable",
"resolve_types",
"setters",
"validate",
"validators",
]
__getattr__ = _make_getattr(__name__)

View File

@ -0,0 +1,314 @@
import sys
from typing import (
Any,
Callable,
Mapping,
Sequence,
overload,
TypeVar,
)
# Because we need to type our own stuff, we have to make everything from
# attr explicitly public too.
from attr import __author__ as __author__
from attr import __copyright__ as __copyright__
from attr import __description__ as __description__
from attr import __email__ as __email__
from attr import __license__ as __license__
from attr import __title__ as __title__
from attr import __url__ as __url__
from attr import __version__ as __version__
from attr import __version_info__ as __version_info__
from attr import assoc as assoc
from attr import Attribute as Attribute
from attr import AttrsInstance as AttrsInstance
from attr import cmp_using as cmp_using
from attr import converters as converters
from attr import Converter as Converter
from attr import evolve as evolve
from attr import exceptions as exceptions
from attr import Factory as Factory
from attr import fields as fields
from attr import fields_dict as fields_dict
from attr import filters as filters
from attr import has as has
from attr import make_class as make_class
from attr import NOTHING as NOTHING
from attr import resolve_types as resolve_types
from attr import setters as setters
from attr import validate as validate
from attr import validators as validators
from attr import attrib, asdict as asdict, astuple as astuple
from attr import NothingType as NothingType
if sys.version_info >= (3, 11):
from typing import dataclass_transform
else:
from typing_extensions import dataclass_transform
_T = TypeVar("_T")
_C = TypeVar("_C", bound=type)
_EqOrderType = bool | Callable[[Any], Any]
_ValidatorType = Callable[[Any, "Attribute[_T]", _T], Any]
_CallableConverterType = Callable[[Any], Any]
_ConverterType = _CallableConverterType | Converter[Any, Any]
_ReprType = Callable[[Any], str]
_ReprArgType = bool | _ReprType
_OnSetAttrType = Callable[[Any, "Attribute[Any]", Any], Any]
_OnSetAttrArgType = _OnSetAttrType | list[_OnSetAttrType] | setters._NoOpType
_FieldTransformer = Callable[
[type, list["Attribute[Any]"]], list["Attribute[Any]"]
]
# FIXME: in reality, if multiple validators are passed they must be in a list
# or tuple, but those are invariant and so would prevent subtypes of
# _ValidatorType from working when passed in a list or tuple.
_ValidatorArgType = _ValidatorType[_T] | Sequence[_ValidatorType[_T]]
@overload
def field(
*,
default: None = ...,
validator: None = ...,
repr: _ReprArgType = ...,
hash: bool | None = ...,
init: bool = ...,
metadata: Mapping[Any, Any] | None = ...,
converter: None = ...,
factory: None = ...,
kw_only: bool | None = ...,
eq: bool | None = ...,
order: bool | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
alias: str | None = ...,
type: type | None = ...,
) -> Any: ...
# This form catches an explicit None or no default and infers the type from the
# other arguments.
@overload
def field(
*,
default: None = ...,
validator: _ValidatorArgType[_T] | None = ...,
repr: _ReprArgType = ...,
hash: bool | None = ...,
init: bool = ...,
metadata: Mapping[Any, Any] | None = ...,
converter: _ConverterType
| list[_ConverterType]
| tuple[_ConverterType, ...]
| None = ...,
factory: Callable[[], _T] | None = ...,
kw_only: bool | None = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
alias: str | None = ...,
type: type | None = ...,
) -> _T: ...
# This form catches an explicit default argument.
@overload
def field(
*,
default: _T,
validator: _ValidatorArgType[_T] | None = ...,
repr: _ReprArgType = ...,
hash: bool | None = ...,
init: bool = ...,
metadata: Mapping[Any, Any] | None = ...,
converter: _ConverterType
| list[_ConverterType]
| tuple[_ConverterType, ...]
| None = ...,
factory: Callable[[], _T] | None = ...,
kw_only: bool | None = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
alias: str | None = ...,
type: type | None = ...,
) -> _T: ...
# This form covers type=non-Type: e.g. forward references (str), Any
@overload
def field(
*,
default: _T | None = ...,
validator: _ValidatorArgType[_T] | None = ...,
repr: _ReprArgType = ...,
hash: bool | None = ...,
init: bool = ...,
metadata: Mapping[Any, Any] | None = ...,
converter: _ConverterType
| list[_ConverterType]
| tuple[_ConverterType, ...]
| None = ...,
factory: Callable[[], _T] | None = ...,
kw_only: bool | None = ...,
eq: _EqOrderType | None = ...,
order: _EqOrderType | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
alias: str | None = ...,
type: type | None = ...,
) -> Any: ...
@overload
@dataclass_transform(field_specifiers=(attrib, field))
def define(
maybe_cls: _C,
*,
these: dict[str, Any] | None = ...,
repr: bool = ...,
unsafe_hash: bool | None = ...,
hash: bool | None = ...,
init: bool = ...,
slots: bool = ...,
frozen: bool = ...,
weakref_slot: bool = ...,
str: bool = ...,
auto_attribs: bool = ...,
kw_only: bool = ...,
cache_hash: bool = ...,
auto_exc: bool = ...,
eq: bool | None = ...,
order: bool | None = ...,
auto_detect: bool = ...,
getstate_setstate: bool | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
field_transformer: _FieldTransformer | None = ...,
match_args: bool = ...,
) -> _C: ...
@overload
@dataclass_transform(field_specifiers=(attrib, field))
def define(
maybe_cls: None = ...,
*,
these: dict[str, Any] | None = ...,
repr: bool = ...,
unsafe_hash: bool | None = ...,
hash: bool | None = ...,
init: bool = ...,
slots: bool = ...,
frozen: bool = ...,
weakref_slot: bool = ...,
str: bool = ...,
auto_attribs: bool = ...,
kw_only: bool = ...,
cache_hash: bool = ...,
auto_exc: bool = ...,
eq: bool | None = ...,
order: bool | None = ...,
auto_detect: bool = ...,
getstate_setstate: bool | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
field_transformer: _FieldTransformer | None = ...,
match_args: bool = ...,
) -> Callable[[_C], _C]: ...
mutable = define
@overload
@dataclass_transform(frozen_default=True, field_specifiers=(attrib, field))
def frozen(
maybe_cls: _C,
*,
these: dict[str, Any] | None = ...,
repr: bool = ...,
unsafe_hash: bool | None = ...,
hash: bool | None = ...,
init: bool = ...,
slots: bool = ...,
frozen: bool = ...,
weakref_slot: bool = ...,
str: bool = ...,
auto_attribs: bool = ...,
kw_only: bool = ...,
cache_hash: bool = ...,
auto_exc: bool = ...,
eq: bool | None = ...,
order: bool | None = ...,
auto_detect: bool = ...,
getstate_setstate: bool | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
field_transformer: _FieldTransformer | None = ...,
match_args: bool = ...,
) -> _C: ...
@overload
@dataclass_transform(frozen_default=True, field_specifiers=(attrib, field))
def frozen(
maybe_cls: None = ...,
*,
these: dict[str, Any] | None = ...,
repr: bool = ...,
unsafe_hash: bool | None = ...,
hash: bool | None = ...,
init: bool = ...,
slots: bool = ...,
frozen: bool = ...,
weakref_slot: bool = ...,
str: bool = ...,
auto_attribs: bool = ...,
kw_only: bool = ...,
cache_hash: bool = ...,
auto_exc: bool = ...,
eq: bool | None = ...,
order: bool | None = ...,
auto_detect: bool = ...,
getstate_setstate: bool | None = ...,
on_setattr: _OnSetAttrArgType | None = ...,
field_transformer: _FieldTransformer | None = ...,
match_args: bool = ...,
) -> Callable[[_C], _C]: ...
class ClassProps:
# XXX: somehow when defining/using enums Mypy starts looking at our own
# (untyped) code and causes tons of errors.
Hashability: Any
KeywordOnly: Any
is_exception: bool
is_slotted: bool
has_weakref_slot: bool
is_frozen: bool
# kw_only: ClassProps.KeywordOnly
kw_only: Any
collected_fields_by_mro: bool
added_init: bool
added_repr: bool
added_eq: bool
added_ordering: bool
# hashability: ClassProps.Hashability
hashability: Any
added_match_args: bool
added_str: bool
added_pickling: bool
on_setattr_hook: _OnSetAttrType | None
field_transformer: Callable[[Attribute[Any]], Attribute[Any]] | None
def __init__(
self,
is_exception: bool,
is_slotted: bool,
has_weakref_slot: bool,
is_frozen: bool,
# kw_only: ClassProps.KeywordOnly
kw_only: Any,
collected_fields_by_mro: bool,
added_init: bool,
added_repr: bool,
added_eq: bool,
added_ordering: bool,
# hashability: ClassProps.Hashability
hashability: Any,
added_match_args: bool,
added_str: bool,
added_pickling: bool,
on_setattr_hook: _OnSetAttrType,
field_transformer: Callable[[Attribute[Any]], Attribute[Any]],
) -> None: ...
@property
def is_hashable(self) -> bool: ...
def inspect(cls: type) -> ClassProps: ...

View File

@ -0,0 +1,3 @@
# SPDX-License-Identifier: MIT
from attr.converters import * # noqa: F403

View File

@ -0,0 +1,3 @@
# SPDX-License-Identifier: MIT
from attr.exceptions import * # noqa: F403

View File

@ -0,0 +1,3 @@
# SPDX-License-Identifier: MIT
from attr.filters import * # noqa: F403

View File

View File

@ -0,0 +1,3 @@
# SPDX-License-Identifier: MIT
from attr.setters import * # noqa: F403

View File

@ -0,0 +1,3 @@
# SPDX-License-Identifier: MIT
from attr.validators import * # noqa: F403

View File

@ -0,0 +1 @@
pip

View File

@ -0,0 +1,20 @@
Copyright 2010 Jason Kirtland
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,60 @@
Metadata-Version: 2.3
Name: blinker
Version: 1.9.0
Summary: Fast, simple object-to-object and broadcast signaling
Author: Jason Kirtland
Maintainer-email: Pallets Ecosystem <contact@palletsprojects.com>
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Classifier: Development Status :: 5 - Production/Stable
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Typing :: Typed
Project-URL: Chat, https://discord.gg/pallets
Project-URL: Documentation, https://blinker.readthedocs.io
Project-URL: Source, https://github.com/pallets-eco/blinker/
# Blinker
Blinker provides a fast dispatching system that allows any number of
interested parties to subscribe to events, or "signals".
## Pallets Community Ecosystem
> [!IMPORTANT]\
> This project is part of the Pallets Community Ecosystem. Pallets is the open
> source organization that maintains Flask; Pallets-Eco enables community
> maintenance of related projects. If you are interested in helping maintain
> this project, please reach out on [the Pallets Discord server][discord].
>
> [discord]: https://discord.gg/pallets
## Example
Signal receivers can subscribe to specific senders or receive signals
sent by any sender.
```pycon
>>> from blinker import signal
>>> started = signal('round-started')
>>> def each(round):
... print(f"Round {round}")
...
>>> started.connect(each)
>>> def round_two(round):
... print("This is round two.")
...
>>> started.connect(round_two, sender=2)
>>> for round in range(1, 4):
... started.send(round)
...
Round 1!
Round 2!
This is round two.
Round 3!
```

View File

@ -0,0 +1,12 @@
blinker-1.9.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
blinker-1.9.0.dist-info/LICENSE.txt,sha256=nrc6HzhZekqhcCXSrhvjg5Ykx5XphdTw6Xac4p-spGc,1054
blinker-1.9.0.dist-info/METADATA,sha256=uIRiM8wjjbHkCtbCyTvctU37IAZk0kEe5kxAld1dvzA,1633
blinker-1.9.0.dist-info/RECORD,,
blinker-1.9.0.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82
blinker/__init__.py,sha256=I2EdZqpy4LyjX17Hn1yzJGWCjeLaVaPzsMgHkLfj_cQ,317
blinker/__pycache__/__init__.cpython-312.pyc,,
blinker/__pycache__/_utilities.cpython-312.pyc,,
blinker/__pycache__/base.cpython-312.pyc,,
blinker/_utilities.py,sha256=0J7eeXXTUx0Ivf8asfpx0ycVkp0Eqfqnj117x2mYX9E,1675
blinker/base.py,sha256=QpDuvXXcwJF49lUBcH5BiST46Rz9wSG7VW_p7N_027M,19132
blinker/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0

View File

@ -0,0 +1,4 @@
Wheel-Version: 1.0
Generator: flit 3.10.1
Root-Is-Purelib: true
Tag: py3-none-any

View File

@ -0,0 +1,17 @@
from __future__ import annotations
from .base import ANY
from .base import default_namespace
from .base import NamedSignal
from .base import Namespace
from .base import Signal
from .base import signal
__all__ = [
"ANY",
"default_namespace",
"NamedSignal",
"Namespace",
"Signal",
"signal",
]

View File

@ -0,0 +1,64 @@
from __future__ import annotations
import collections.abc as c
import inspect
import typing as t
from weakref import ref
from weakref import WeakMethod
T = t.TypeVar("T")
class Symbol:
"""A constant symbol, nicer than ``object()``. Repeated calls return the
same instance.
>>> Symbol('foo') is Symbol('foo')
True
>>> Symbol('foo')
foo
"""
symbols: t.ClassVar[dict[str, Symbol]] = {}
def __new__(cls, name: str) -> Symbol:
if name in cls.symbols:
return cls.symbols[name]
obj = super().__new__(cls)
cls.symbols[name] = obj
return obj
def __init__(self, name: str) -> None:
self.name = name
def __repr__(self) -> str:
return self.name
def __getnewargs__(self) -> tuple[t.Any, ...]:
return (self.name,)
def make_id(obj: object) -> c.Hashable:
"""Get a stable identifier for a receiver or sender, to be used as a dict
key or in a set.
"""
if inspect.ismethod(obj):
# The id of a bound method is not stable, but the id of the unbound
# function and instance are.
return id(obj.__func__), id(obj.__self__)
if isinstance(obj, (str, int)):
# Instances with the same value always compare equal and have the same
# hash, even if the id may change.
return obj
# Assume other types are not hashable but will always be the same instance.
return id(obj)
def make_ref(obj: T, callback: c.Callable[[ref[T]], None] | None = None) -> ref[T]:
if inspect.ismethod(obj):
return WeakMethod(obj, callback) # type: ignore[arg-type, return-value]
return ref(obj, callback)

Some files were not shown because too many files have changed in this diff Show More