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

Python3.12 compatibility / drop 'future' dependency #742

Closed
umlaeute opened this issue Dec 12, 2023 · 5 comments
Closed

Python3.12 compatibility / drop 'future' dependency #742

umlaeute opened this issue Dec 12, 2023 · 5 comments
Assignees
Labels

Comments

@umlaeute
Copy link

canmatrix depends on future, but it seems that future is not really compatible with Python3.12 (yet?).

One possible solution is to fix future, but

  • this is more complicated than my average python foo
  • future is a compat layer for Py2/Py3, but according to https://github.com/ebroecker/canmatrix/releases/tag/1.0, we've seen the "Last py2 release", so it's probably time to drop cruft.
  • canmatrix uses a single function from future, which can trivially be implemented in ordinary Python

here's a patch that eliminates the use of future, but requires Python3:

--- python-canmatrix.orig/src/canmatrix/formats/sym.py
+++ python-canmatrix/src/canmatrix/formats/sym.py
@@ -100,11 +100,7 @@
 
 def create_signal(db, signal):  # type: (canmatrix.CanMatrix, canmatrix.Signal) -> str
     output = ""
-    if sys.version_info > (3, 0):
-        quote_name = not signal.name.isidentifier()
-    else:
-        from future.utils import isidentifier
-        quote_name = not isidentifier(signal.name)
+    quote_name = not signal.name.isidentifier()
     if quote_name:
         output += 'Var="%s" ' % signal.name
     else:
@umlaeute
Copy link
Author

a more complete solution would be to just drop all those occurences of if sys.version_info <= (3,0)

@umlaeute
Copy link
Author

umlaeute commented Dec 12, 2023

also note that

DeprecationWarning: The SafeConfigParser class has been renamed to ConfigParser in Python 3.2. This alias will be removed in Python 3.12. Use ConfigParser directly instead.

since Python3.12 has arrived, this needs to be handled somehow:

--- python-canmatrix.orig/versioneer.py
+++ python-canmatrix/versioneer.py
@@ -336,9 +336,9 @@
     # configparser.NoOptionError (if it lacks "VCS="). See the docstring at
     # the top of versioneer.py for instructions on writing your setup.cfg .
     setup_cfg = os.path.join(root, "setup.cfg")
-    parser = configparser.SafeConfigParser()
+    parser = configparser.ConfigParser()
     with open(setup_cfg, "r") as f:
-        parser.readfp(f)
+        parser.read_file(f)
     VCS = parser.get("versioneer", "VCS")  # mandatory
 
     def get(parser, name):

this patch requires Python>=3.2 which is probably acceptable (or not)

@umlaeute
Copy link
Author

in my original post, i missed that there are a few imports of the past library (which is part of future),

so here's a more complete patch to get rid of future:

Index: python-canmatrix-1.0~github/setup.py
===================================================================
--- python-canmatrix-1.0~github.orig/setup.py
+++ python-canmatrix-1.0~github/setup.py
@@ -83,7 +83,6 @@ setup(
         "attrs>=19.2.0",
         "click",
         "enum34; python_version < '3.4'",
-        "future",
         "importlib-metadata; python_version < '3.8'",
         "six",
         "typing; python_version < '3.5'",
Index: python-canmatrix-1.0~github/src/canmatrix/formats/sym.py
===================================================================
--- python-canmatrix-1.0~github.orig/src/canmatrix/formats/sym.py
+++ python-canmatrix-1.0~github/src/canmatrix/formats/sym.py
@@ -100,11 +100,7 @@ def format_float(f):  # type: (typing.An
 
 def create_signal(db, signal):  # type: (canmatrix.CanMatrix, canmatrix.Signal) -> str
     output = ""
-    if sys.version_info > (3, 0):
-        quote_name = not signal.name.isidentifier()
-    else:
-        from future.utils import isidentifier
-        quote_name = not isidentifier(signal.name)
+    quote_name = not signal.name.isidentifier()
     if quote_name:
         output += 'Var="%s" ' % signal.name
     else:
Index: python-canmatrix-1.0~github/src/canmatrix/canmatrix.py
===================================================================
--- python-canmatrix-1.0~github.orig/src/canmatrix/canmatrix.py
+++ python-canmatrix-1.0~github/src/canmatrix/canmatrix.py
@@ -40,7 +40,6 @@ import warnings
 from builtins import *
 
 import attr
-from past.builtins import basestring
 from six.moves import zip_longest
 
 import canmatrix.copy
@@ -432,7 +431,7 @@ class Signal(object):
             if not (self.min <= value <= self.max):
                 value = self.min
 
-        if isinstance(value, basestring):
+        if isinstance(value, str):
             for value_key, value_string in self.values.items():
                 if value_string == value:
                     value = value_key
Index: python-canmatrix-1.0~github/src/canmatrix/formats/xls.py
===================================================================
--- python-canmatrix-1.0~github.orig/src/canmatrix/formats/xls.py
+++ python-canmatrix-1.0~github/src/canmatrix/formats/xls.py
@@ -30,7 +30,6 @@ import logging
 import typing
 from builtins import *
 
-import past.builtins
 import xlrd
 import xlwt
 
@@ -540,7 +539,7 @@ def load(file, **options):
         unit = ""
 
         factor = sh.cell(row_num, index['function']).value
-        if isinstance(factor, past.builtins.basestring):
+        if isinstance(factor, str):
             factor = factor.strip()
             if " " in factor and factor[0].isdigit():
                 (factor, unit) = factor.strip().split(" ", 1)
Index: python-canmatrix-1.0~github/src/canmatrix/formats/yaml.py
===================================================================
--- python-canmatrix-1.0~github.orig/src/canmatrix/formats/yaml.py
+++ python-canmatrix-1.0~github/src/canmatrix/formats/yaml.py
@@ -30,7 +30,6 @@ import typing
 from builtins import *
 
 import yaml
-from past.builtins import long, unicode
 
 import canmatrix
 
@@ -43,8 +42,6 @@ except ImportError:
 representers = False
 try:
     yaml.add_representer(int, SafeRepresenter.represent_int)
-    yaml.add_representer(long, SafeRepresenter.represent_long)
-    yaml.add_representer(unicode, SafeRepresenter.represent_unicode)
     yaml.add_representer(str, SafeRepresenter.represent_unicode)
     yaml.add_representer(list, SafeRepresenter.represent_list)
     representers = True
@@ -67,7 +64,7 @@ def dump(db, f, **options):  # type: (ca
 
     # f = open(filename, "w")
     if representers:
-        f.write(unicode(yaml.dump(new_db)))
+        f.write(yaml.dump(new_db))
     else:
         f.write(yaml.dump(new_db).encode('utf8'))
 
Index: python-canmatrix-1.0~github/mypy.ini
===================================================================
--- python-canmatrix-1.0~github.orig/mypy.ini
+++ python-canmatrix-1.0~github/mypy.ini
@@ -34,5 +34,5 @@ ignore_errors = True
 ignore_errors = True
 
 # other settings:
-[mypy-xlsxwriter,past,past.builtins,pathlib2]
+[mypy-xlsxwriter,pathlib2]
 ignore_missing_imports = True

notes

  • the patch in this comment is only concerned about the future module (and not about the configparser in the other comment)
  • i've replaced isinstance(x, basestring) with isinstance(x, str), as basestring was typically used to check whether a value was either str or unicode (which are both str in Py3). However, IIRC basestring is also the base class for bytes (at least isinstance(b'foo', past.builtins.basestring) yields True), so the new test might miss some cases...

ebroecker added a commit that referenced this issue Dec 13, 2023
ebroecker added a commit that referenced this issue Dec 13, 2023
@ebroecker
Copy link
Owner

Hi @umlaeute ,

thanks for your help.

And yes, you are right.
I created 2 pull requests for your patches:
#744
#745

Thanks again

@umlaeute
Copy link
Author

sorry to have created additional work.

i could (and would) of course have done the PRs myself, but wanted to get your feedback first to see if i was on the right track (and it was easier for my actual workflow (while preparing the Debian packages) to just paste the diffs for now)

ebroecker added a commit that referenced this issue Dec 13, 2023
ebroecker added a commit that referenced this issue Dec 18, 2023
@ebroecker ebroecker self-assigned this Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants