Skip to main content

Runtime final guard for methods/properties across projects

Project description

Access Guard

一個極輕量的 runtime 防護工具,提供 @final 裝飾器與 Access metaclass, 阻止被標註為 final 的方法 / property 在子類中被覆寫,並防止類別建立後的動態覆寫。

特點

  • 支援普通方法、@staticmethod@classmethod@property (含 getter/setter/deleter)。
  • 不依賴第三方套件,僅使用標準函式庫。
  • 在類別建立階段即檢查,違規直接拋出 RuntimeError
  • 防止猴子補丁:類別建立後再次指派 final 成員也會被阻擋。

安裝

pip install access-guard

快速示例

from access_guard import Access, final

class Base(metaclass=Access):
	@final
	def stable(self):
		return "v1"

	@property
	@final
	def value(self):
		return 42

class Child(Base):
	pass

Child().stable()  # OK

# 下列行為任一種將觸發 RuntimeError:
# class Bad(Base):
#     def stable(self):  # 覆寫 final 方法
#         return "v2"

# Bad.stable = lambda self: "hack"   # 類別建立後動態覆寫

行為細節

  1. @final 會在實際函式物件上標記特殊屬性;對 property 會同時標記其 accessor。
  2. Access metaclass 在 __new__ 中收集所有基類的 final 名稱並檢查是否違規覆寫。
  3. 類別建立後再次透過賦值覆寫 final 名稱會在 __setattr__ 被拒絕。
  4. 目前不阻擋魔術方法 (如 __init__),若要啟用可修改 final.py_collect_final_names_from_bases 的條件。

限制 / 注意事項

  • 僅在 class 定義與動態賦值層級防護;不處理 instance 屬性。
  • 不追蹤別名引用 (你仍可複製函式物件再指派為其他名稱)。
  • 與多重繼承共用時,若多個基類定義不同成員名稱,照常運作;如果名稱衝突且某基類標為 final,子類不可覆寫。

測試

安裝開發相依後執行:

pip install -e .[dev]
pytest -q

版本策略

採語義化版本。初期階段 (<1.0.0) 可能進行破壞式調整。

授權

MIT License,詳見 LICENSE

未來規劃

  • 提供 final_class 裝飾器,一次鎖定整個類別所有成員。
  • 增加型別註解與 mypy plugin (讓靜態分析工具也能偵測)。
  • CI 自動化與發佈工作流程。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

access_guard-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

access_guard-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file access_guard-0.1.0.tar.gz.

File metadata

  • Download URL: access_guard-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for access_guard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 34defe64d5473c8abd448d99cfed84b598dc6dbafaece1c48337c0a6891ba089
MD5 338757bd1eecf02c897916250a65b425
BLAKE2b-256 2d7965d65cf50768956a4ba5a2aa843597115ece8b14f503f6ea58b7161640c6

See more details on using hashes here.

File details

Details for the file access_guard-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: access_guard-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for access_guard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e4edc79303921015ad7b6e843e3c1c312ab11800f870224b01c83a2c57154d0
MD5 a80d0dc2bf80e590c661f900db35f273
BLAKE2b-256 6b830ebed2ad26fb669839d478e47c3485362b98d7060b6e1bee00ceba4f38a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page