Repository: cogent3/cogent3 (language: python)

Issue:
`load_tree` doesn't work on specific file input
`load_tree` no longer works on a specific tree file. Below is an example file:

[bad.txt](https://github.com/user-attachments/files/16576860/bad.txt)

Running the following:
```python
from cogent3 import load_tree
load_tree("bad.txt")
```

Gives the following error:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rmcar17/miniconda3/envs/scs/lib/python3.12/site-packages/cogent3/__init__.py", line 752, in load_tree
    return make_tree(treestring, format=format, underscore_unmunge=underscore_unmunge)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rmcar17/miniconda3/envs/scs/lib/python3.12/site-packages/cogent3/__init__.py", line 709, in make_tree
    tree = parser(treestring, tree_builder, underscore_unmunge=underscore_unmunge)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rmcar17/miniconda3/envs/scs/lib/python3.12/site-packages/cogent3/parse/newick.py", line 149, in parse_string
    raise TreeParseError(f'Not a Newick tree: "{text[:10]}"')
cogent3.parse.newick.TreeParseError: Not a Newick tree: "愨戬㬩"
```


It did work in the `2024.7.19a1` release but failed somewhere between that and the `2024.7.19a3` release.


Discussion / hints:
This is an issue with `charset-normalizer`, solution is to revert to `chardet` for now

Fix patch:
diff --git a/changelog.md b/changelog.md
index 88f27977d..c2ce06ece 100755
--- a/changelog.md
+++ b/changelog.md
@@ -1,4 +1,17 @@
 
+<a id='changelog-2024.7.19a4'></a>
+# Changes in release "2024.7.19a4"
+
+This is a minor bug fix release.
+
+## Contributors
+
+- Contribution from @rmcar17 on fixing an encoding error when loading small newick trees.
+
+## BUG
+
+- Fixed an issue with `load_tree` incorrectly detecting the encoding of very small newick files.
+
 <a id='changelog-2024.7.19a3'></a>
 # Changes in release "2024.7.19a3"
 
diff --git a/pyproject.toml b/pyproject.toml
index 0b7281c12..2fb9f9b41 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -13,7 +13,7 @@ authors = [
 keywords = ["biology", "genomics", "statistics", "phylogeny", "evolution", "bioinformatics"]
 readme = "README.md"
 license = { file = "LICENSE" }
-dependencies = ["charset-normalizer",
+dependencies = ["chardet",
         "numpy",
         "numba>0.53; python_version>='3.9' and python_version <'3.12'",
         "numba>0.54; python_version>='3.10' and python_version <'3.12'",
diff --git a/src/cogent3/_version.py b/src/cogent3/_version.py
index dda4d6761..4cb87df4d 100644
--- a/src/cogent3/_version.py
+++ b/src/cogent3/_version.py
@@ -1,2 +1,2 @@
 # remember to update version in pyproject.toml too!
-__version__ = "2024.7.19a3"
+__version__ = "2024.7.19a4"
diff --git a/src/cogent3/util/io.py b/src/cogent3/util/io.py
index 6f34a1afb..1c9891a6a 100644
--- a/src/cogent3/util/io.py
+++ b/src/cogent3/util/io.py
@@ -13,7 +13,7 @@
 from urllib.request import urlopen
 from zipfile import ZipFile
 
-from charset_normalizer import detect
+from chardet import detect
 
 from cogent3.util.misc import _wout_period
 


Test patch:
diff --git a/tests/test_core/test_tree.py b/tests/test_core/test_tree.py
index 2c5019703..8e4f3c25b 100644
--- a/tests/test_core/test_tree.py
+++ b/tests/test_core/test_tree.py
@@ -2316,3 +2316,18 @@ def test_parser():
     tidied = tree.get_newick(with_distances=1)
     assert tidied == nice
     assert tree.get_node_matching_name("pair").params["other"] == ["com\nment"]
+
+
+def test_load_tree_bad_encoding():
+    """
+    charset_normalizer rather than chardet as a dependency
+    incorrectly detected the encoding of a file as UTF-16LE.
+    """
+    newick = "(a,b);"
+
+    with TemporaryDirectory(dir=".") as dirname:
+        tree_path = os.path.join(dirname, "tree.tree")
+        with open(tree_path, "wb") as f:
+            f.write(newick.encode("ascii"))
+
+        assert load_tree(tree_path).get_newick() == newick