Fix #23: Make Field.Access NotIterable
Why?
Sympy does not try to iterate when the object is NotIterable
. If it has __iter__
but iter throws it will only sometimes think its not iterable.
Merge request reports
Activity
https://github.com/sympy/sympy/pull/19772/files
Maybe we can backport that version of the function for older Sympy version if the functionality is important.
assigned to @kuron
Yes, that's exactly the problem. In most cases people forget to exclude
NotIterable
.Throwing
TypeError
will makeiterable
alwaysFalse
but other parts of Sympy thinkField.Access
is iterable because it has__iter__
. Pythoniter
works without a__iter__
method by simply using__getitem__[0]
,__getitem__[1] ...
So in summary there's no way to tell Sympy that something is not iterable.
@hu62bodi enjoy, maybe first look at the issue.
Edited by Stephan SeitzI think I found a solution: adding a magic
_iterable = False
member. Will try that out.This is our problem
class Test8(NotIterable): def __getitem__(self, *idx): return "something" assert iter(Test8()) assert iterable(Test8()) is False assert iterable(Test8(), exclude=str) is False # will fail without my PR
will add that test to the PR.
Our issue can be solved by
class Test8(NotIterable): _iterable = False def __getitem__(self, *idx): return "something" assert iter(Test8()) assert iterable(Test8()) is False assert iterable(Test8(), exclude=str) is False # correct behavior without this Sympy PR.
Edited by Stephan Seitzadded 1 commit
- f047af73 - Fix #23 (closed): Make Field.Access NotIterable
added 1 commit
- be52c6e1 - Fix #23 (closed): Make Field.Access NotIterable
Good idea.
Edited by Stephan Seitzadded 35 commits
-
be52c6e1...558b9f81 - 34 commits from branch
pycodegen:master
- f4ba5448 - Fix #23 (closed): Make Field.Access NotIterable
-
be52c6e1...558b9f81 - 34 commits from branch
mentioned in commit 04eb30b6