Prevent an implementation for `int` class from operating on `bool` values
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start by reproducing the example with classes 0.4.0, then trace dispatch from the typeclass declaration and the render.instance(int) registration. Done means render(True) no longer uses the int implementation and instead raises NotImplementedError for the unspecified bool case.
Written by the indexing model from the issue text.
Description
Context. Consider the following piece of code.
from classes import typeclass
@typeclass
def render(data_value) -> str:
"""Pretty-print a value."""
@render.instance(int)
def _render_int(data_value: int) -> str:
return f'🔢 {data_value}'
render(True) == '🔢 True'
(this should be runnable as-is on classes 0.4.0.)
I would have expected this code to fail with a NotImplementedError because the bool case wasn't specified; but instead, that case is handled by the int implementation because
In [3]: issubclass(bool, int)
Out[3]: True
Decision. In an int implementation, recognize if the provided value is actually a bool and refuse to process that value.
Consequences. I have been writing in Python for quite a few years now and I might even have encountered this relationship between int and bool types before, but that is not quite a type of thing that I keep in my short-term memory to be able to instantly recognize.
I believe this change can help avert bugs which might be hard to trace otherwise.
- Dominant language
- Python
- Stars
- 730
- Forks
- 30
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dry-python/classes
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
dry-python/classes#496 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
dry-python/classes#494 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 52/100
dry-python/classes#492 · 2 comments ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 35/100
dry-python/classes#481 · 5 comments · 4 reactions ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
dry-python/classes#386 ·
All issues in dry-python/classes
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
-
hcocena Openpolicies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Bioconductor/BiocContributions#214 · 5 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
TencentCloud/Octop#1169 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
521xueweihan/HelloGitHub#3778 ·
-
The version checker's trailing attribute region has no control for a less-than inside a quoted value Openarea: dashboard area: tests bug perceived difficulty: 2 python
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Nitjsefnie-Harness-Commons/daedalus#1105 · 1 comment ·