Repository: dbt-labs/dbt-core

Issue:
Investigate and fix deprecation warnings encountered in tests
When we run our tests we get a fairly large number of deprecation warnings. Many of these seem to come from dependencies. We should investigate these warnings and see if we should 1) upgrade the dependency or 2) modify our code or 2) just wait.

Here are some examples of warnings. We should run the tests and collect all of them for investigation.

env/lib/python3.8/site-packages/packaging/version.py:127: DeprecationWarning: Creating a LegacyVersion has been deprecated and will be removed in the next major release

jinja2/runtime.py:679: DeprecationWarning: 'soft_unicode' has been renamed to 'soft_str'. The old name will be removed in MarkupSafe 2.1.

<class 'networkx.utils.decorators.argmap'> compilation 5:5: DeprecationWarning: write_gpickle is deprecated and will be removed in 3.0.Use ``pickle.dump(G, path, protocol)``




Discussion:

Resolving patch:
diff --git a/core/dbt/compilation.py b/core/dbt/compilation.py
--- a/core/dbt/compilation.py
+++ b/core/dbt/compilation.py
@@ -3,6 +3,7 @@
 from typing import List, Dict, Any, Tuple, cast, Optional
 
 import networkx as nx  # type: ignore
+import pickle
 import sqlparse
 
 from dbt import flags
@@ -162,7 +163,8 @@ def write_graph(self, outfile: str, manifest: Manifest):
         for node_id in self.graph:
             data = manifest.expect(node_id).to_dict(omit_none=True)
             out_graph.add_node(node_id, **data)
-        nx.write_gpickle(out_graph, outfile)
+        with open(outfile, 'wb') as outfh:
+            pickle.dump(out_graph, outfh, protocol=pickle.HIGHEST_PROTOCOL)
 
 
 class Compiler:
diff --git a/core/dbt/graph/graph.py b/core/dbt/graph/graph.py
--- a/core/dbt/graph/graph.py
+++ b/core/dbt/graph/graph.py
@@ -31,11 +31,13 @@ def ancestors(
         """Returns all nodes having a path to `node` in `graph`"""
         if not self.graph.has_node(node):
             raise InternalException(f'Node {node} not found in the graph!')
-        with nx.utils.reversed(self.graph):
-            anc = nx.single_source_shortest_path_length(G=self.graph,
-                                                        source=node,
-                                                        cutoff=max_depth)\
-                .keys()
+        # This used to use nx.utils.reversed(self.graph), but that is deprecated,
+        # so changing to use self.graph.reverse(copy=False) as recommeneded
+        G = self.graph.reverse(copy=False) if self.graph.is_directed() else self.graph
+        anc = nx.single_source_shortest_path_length(G=G,
+                                                    source=node,
+                                                    cutoff=max_depth)\
+            .keys()
         return anc - {node}
 
     def descendants(
diff --git a/core/dbt/semver.py b/core/dbt/semver.py
--- a/core/dbt/semver.py
+++ b/core/dbt/semver.py
@@ -1,5 +1,6 @@
 from dataclasses import dataclass
 import re
+import warnings
 from typing import List
 
 from packaging import version as packaging_version
@@ -145,10 +146,13 @@ def compare(self, other):
                     return 1
                 if b is None:
                     return -1
-            if packaging_version.parse(a) > packaging_version.parse(b):
-                return 1
-            elif packaging_version.parse(a) < packaging_version.parse(b):
-                return -1
+            # This suppresses the LegacyVersion deprecation warning
+            with warnings.catch_warnings():
+                warnings.simplefilter("ignore", category=DeprecationWarning)
+                if packaging_version.parse(a) > packaging_version.parse(b):
+                    return 1
+                elif packaging_version.parse(a) < packaging_version.parse(b):
+                    return -1
 
         equal = ((self.matcher == Matchers.GREATER_THAN_OR_EQUAL and
                   other.matcher == Matchers.LESS_THAN_OR_EQUAL) or


Test patch:
diff --git a/test/unit/test_graph.py b/test/unit/test_graph.py
--- a/test/unit/test_graph.py
+++ b/test/unit/test_graph.py
@@ -28,7 +28,6 @@
 class GraphTest(unittest.TestCase):
 
     def tearDown(self):
-        self.write_gpickle_patcher.stop()
         self.mock_filesystem_search.stop()
         self.mock_hook_constructor.stop()
         self.load_state_check.stop()
@@ -58,13 +57,6 @@ def setUp(self):
             {n.unique_id: n for n in generate_name_macros('test_models_compile')})
         self.mock_models = []  # used by filesystem_searcher
 
-        # Create gpickle patcher
-        self.write_gpickle_patcher = patch('networkx.write_gpickle')
-        def mock_write_gpickle(graph, outfile):
-            self.graph_result = graph
-        self.mock_write_gpickle = self.write_gpickle_patcher.start()
-        self.mock_write_gpickle.side_effect = mock_write_gpickle
-
         # Create file filesystem searcher
         self.filesystem_search = patch('dbt.parser.read_files.filesystem_search')
         def mock_filesystem_search(project, relative_dirs, extension):
@@ -139,7 +131,10 @@ def get_config(self, extra_cfg=None):
         }
         cfg.update(extra_cfg)
 
-        return config_from_parts_or_dicts(project=cfg, profile=self.profile)
+        config = config_from_parts_or_dicts(project=cfg, profile=self.profile)
+        dbt.flags.set_from_args({}, config)
+        dbt.flags.PARTIAL_PARSE = False
+        return config
 
     def get_compiler(self, project):
         return dbt.compilation.Compiler(project)