python / python/typing

Monkey patching support

未关闭
#718 7 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

topic: feature
主要语言
Python
星标
1.8k
派生
302
平均合并
23 小时
30 天内合并 PR
8

描述

These are just some not terribly well though out ideas about possible monkey support in typing. This would need a proper PEP if it was ever implemented, but I just want to record my ideas here:

# foo accepts a date object, but allows to add and access
# unknown attributes at runtime. It basically treats date
# as having __getattr__()/__setattr__() methods.
def foo(date: Dynamic[datetime.date]) -> None:
    date.my_attr = 123
    print(date.added_by_called)


# typing.dynamic() is a no-op at runtime and basically an alias
# for cast(Generic[X], y) at type-check time, where X is the
# type of y.
# In this example, dt has type Dynamic[datetime.date].
dt = dynamic(datetime.date.today())
foo(dt)

# The following would also work:
foo(datetime.date.today())
other_dt: datetime.date = dt

A possible extensions would be to define the allowed monkey-patched attributes. Something like:

class Monkey(Protocol):
    donkey: int

def foo(dt: Dynamic[datetime.date, Monkey]) -> None:
    # ok:
    print(dt.year)
    dt.donkey += 3
    # errors:
    print(dt.kong)
    dt.donkey += ""

dt = dynamic(datetime.date.today(), Monkey)

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先审阅提议的 Dynamic 和 dynamic 示例,包括基于 Protocol 的允许属性扩展,然后阅读包含七条评论的讨论,以了解其中的约束或异议。此 issue 旨在探索,而不是已经可以实施;要完成它,需要先达成一致的设计,并完成 issue 中提到的适当 PEP,之后才能开始代码工作。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
devtools
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。