Closed 3a5ab40a-5e15-47f8-8025-1665d51c0660 closed 7 years ago
Description changed:
---
+++
@@ -3,6 +3,6 @@
But the code has to depend on the Python version!
-There are 30 effected modules.
+There are 30 affected modules.
This ticket is tracked as a dependency of meta-ticket ticket:16052.
Description changed:
---
+++
@@ -6,3 +6,5 @@
There are 30 affected modules.
This ticket is tracked as a dependency of meta-ticket ticket:16052.
+
+REFERENCE: http://stackoverflow.com/questions/18513821/python-metaclass-understanding-the-with-metaclass
first half in #22474 (combinat folder)
another easy step in #22479
then there remains more difficult cases
Cython files: #22537
@tscrim: I have not been able to fix the use of metaclass in
* src/sage/algebras/clifford_algebra.py
* src/sage/algebras/commutative_dga.py
* src/sage/modular/modform_hecketriangle/graded_ring_element.py
The expected python3 replacement is
from six import add_metaclass
@add_metaclass(name_of_metaclass)
class something
But this does not work for the three mentioned files.
For reference, can you at least push the non-working branch?
Branch: public/16074
failing as follows
File "/home/chapoton/sage/local/lib/python2.7/site-packages/sage/algebras/clifford_algebra.py", line 2163, in <module>
class ExteriorAlgebraDifferential(ModuleMorphismByLinearity, UniqueRepresentation):
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
@add_metaclass
probably does something with the metaclass. Unless it is because of our heirarchy of metaclass...but I doubt it is that. It might be a decorator that needs to be added to the subclasses as well.
The problem is the decorator, because it works in 2 steps:
The class is created the usual way.
The decorator modifies the class to have a new metaclass.
The problem is that step 1. already fails, so the decorator cannot do anything.
I'll look into this.
Replying to @fchapoton:
@tscrim: I have not been able to fix the use of metaclass in
* src/sage/algebras/clifford_algebra.py * src/sage/algebras/commutative_dga.py * src/sage/modular/modform_hecketriangle/graded_ring_element.py
There are some other files with __metaclass__
, such as src/sage/structure/unique_representation.py
. Should I try to fix these on this ticket too or is there a different ticket?
Please fix the metaclass issue in all places where you can. There is no other ticket. I only listed the algebraic use cases. The rest is more like "sage infrastructure".
Hmm, I agree that it's not easy :-) I'm making some progress, but I'm not quite there yet.
Author: Jeroen Demeyer
Done. The main effort here is fixing sage.misc.six.with_metaclass
(the first of 3 commits).
After this ticket, there is no remaining Sage code using __metaclass__
. Some doctests still use __metaclass__
, but I suggest to fix that later.
I don't agree with this change (and subsequent changes from this):
diff --git a/src/sage/misc/six.py b/src/sage/misc/six.py
index 8273d91..5f03a96 100644
--- a/src/sage/misc/six.py
+++ b/src/sage/misc/six.py
@@ -8,7 +8,7 @@ from __future__ import absolute_import
from six import *
-def with_metaclass(meta, *bases):
+def with_metaclass(*args):
"""
Create a base class with a metaclass.
as later we require the first argument to be the meta class. So if someone happened to pass nothing, it would get an error message starting much later in the code. IMO, it also obfuscates the code a little bit too.
Branch pushed to git repo; I updated commit sha1. New commits:
f1ca05f | Restore (meta, *bases) arguments for with_metaclass() |
I wanted to submit these fixes to with_metaclass
upstream to six
. However, when looking at the original six
code, I understood what was going wrong and it is really simple.
I also realized that it was #18503 which is the cause for the metaclass breakage. In other words, #18503 did the wrong thing and actually made everything more complicated.
It turns out that #18503 can be fixed in a much simpler way, see https://github.com/benjaminp/six/pull/191 and then this issue (#16074) simply does not occur.
That being said, I think the current solution on this ticket is structurally better than upstream's with_metaclass
: upstream is overriding __new__
with __call__
which is a bit fishy. Instead, I am overriding __call__
with __call__
which makes more sense.
Makes sense to me. It's somewhat less pretty than the decorator, but this works for now and gets us closer to Python3.
Reviewer: Travis Scrimshaw
Changed branch from public/16074 to f1ca05f
The tool 2to3 changes the code to the new Py3 syntax.
But the code has to depend on the Python version!
There are 30 affected modules.
This ticket is tracked as a dependency of meta-ticket ticket:16052.
REFERENCE: http://stackoverflow.com/questions/18513821/python-metaclass-understanding-the-with-metaclass
CC: @tscrim @jdemeyer
Component: python3
Author: Jeroen Demeyer
Branch/Commit:
f1ca05f
Reviewer: Travis Scrimshaw
Issue created by migration from https://trac.sagemath.org/ticket/16074