Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(overloading): Fixed behavior where overloaded __setattr__ did not… #4

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- mode: conf; -*-
## flake8 default linting configuration
#
# Loaded by flake8 by default

[flake8]
# DO NOT check for the following errors:

# E266: too many leading '#' for block comment
# E722: do not use bare except, specify exception instead
# W503: Line breaks should occur after the binary operator
# to keep all variable names aligned. (we're doing the opposite)
# C901 too complex, change this later
ignore=E266,E722,W503,C901

; exclude=

# F401: module imported but unused
# F403 unable to detect undefined names
per-file-ignores =
*/__init__.py:F401,F403

# 120 characters is a more agreeable max line length for modern displays
max-line-length=127
41 changes: 41 additions & 0 deletions .github/workflows/complete_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Complete Build, Lint, and Test

on:
pull_request:
branches:
- "main"
workflow_dispatch:

permissions:
contents: read

jobs:
complete:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.12
uses: actions/setup-python@v3
with:
python-version: "3.12"

- name: Install package
run: |
python -m pip install --upgrade pip
python -m pip install wheel
python -m pip install flake8
python -m pip install pytest
python -m pip install .

- name: Lint with flake8
run: |
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 ./src --count --max-complexity=10 --max-line-length=127 --statistics

- name: Test with pytest
run: |
pytest ./test
2 changes: 1 addition & 1 deletion src/itemattribute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import importlib.resources
import json
import os
import os


dir = os.path.dirname(os.path.abspath(__file__))
Expand Down
20 changes: 17 additions & 3 deletions src/itemattribute/item_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,23 @@ def __init__(self, dictionary=None):
for k in dictionary.keys():
self[k] = dictionary[k]

__getitem__ = object.__getattribute__
__setitem__ = object.__setattr__
__delitem__ = object.__delattr__
def __getitem__(self, key):
'''
Maps self[key] to the self.__getattribute__ method.
'''
return self.__getattribute__(key)

def __setitem__(self, key, value):
'''
Maps self[key] = value to the self.__setattr__ method.
'''
self.__setattr__(key, value)

def __delitem__(self, key):
'''
Maps del self[key] to the self.__delattr__ method.
'''
self.__delattr__(key)

def keys(self):
'''
Expand Down
96 changes: 96 additions & 0 deletions test/test_itemattribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import pytest
from itemattribute import ItemAttribute


def test_set_item():
ia = ItemAttribute()
ia['a'] = 1
assert ia.a == 1, 'Set item failed to assign to attribute'


def test_set_int_error():
with pytest.raises(TypeError):
ia = ItemAttribute()
ia[1] = 1


def test_set_attribute():
ia = ItemAttribute()
ia.a = 1
assert ia['a'] == 1, 'Set attribute failed to assign to attribute'


def test_del_item():
ia = ItemAttribute()
ia['a'] = 1
del ia['a']
with pytest.raises(AttributeError):
assert ia.a, 'Del item failed to remove attribute'


def test_del_attribute():
ia = ItemAttribute()
ia.a = 1
del ia.a
with pytest.raises(AttributeError):
assert ia['a'], 'Del attribute failed to remove item'


def test_keys():
ia = ItemAttribute()
ia.a = 1
ia.b = 2
assert set(ia.keys()) == {'a', 'b'}, 'Keys failed to return keys'

ia = ItemAttribute()
ia['a'] = 1
ia['b'] = 2
assert set(ia.keys()) == {'a', 'b'}, 'Keys failed to return keys'


def test_values():
ia = ItemAttribute()
ia.a = 1
ia.b = 2
assert set(ia.values()) == {1, 2}, 'Values failed to return values'

ia = ItemAttribute()
ia['a'] = 1
ia['b'] = 2
assert set(ia.values()) == {1, 2}, 'Values failed to return values'


def test_items():
ia = ItemAttribute()
ia.a = 1
ia.b = 2
assert set(ia.items()) == {('a', 1), ('b', 2)}, 'Items failed to return items'

ia = ItemAttribute()
ia['a'] = 1
ia['b'] = 2
assert set(ia.items()) == {('a', 1), ('b', 2)}, 'Items failed to return items'


def test_contains():
ia = ItemAttribute()
ia.a = 1
assert 'a' in ia, 'Contains failed to return True'

ia = ItemAttribute()
ia['a'] = 1
assert 'a' in ia, 'Contains failed to return True'


def test_setattr_overload():

class NewItemAttribute():
def __setattr__(self, key, value):
if isinstance(value, (int, float)):
self.__dict__[key + 'p1'] = value + 1
self.__dict__[key] = value

newia = NewItemAttribute()
newia.a = 1
assert newia.a == 1, 'Set attribute failed to assign to attribute'
assert newia.ap1 == 2, 'Set attribute failed to assign extra attribute'
Loading