Repository: tskit-dev/msprime (language: python)

Issue:
Update deps, remove py37 and fix test warnings



Discussion / hints:

Fix patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 26c74690c..ea4867ade 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -1,6 +1,6 @@
 repos:
   - repo: https://github.com/pre-commit/pre-commit-hooks
-    rev: v4.3.0
+    rev: v4.5.0
     hooks:
       - id: check-merge-conflict
       - id: debug-statements
@@ -14,23 +14,23 @@ repos:
       exclude: argtable3
       verbose: true
   - repo: https://github.com/asottile/reorder_python_imports
-    rev: v3.9.0
+    rev: v3.12.0
     hooks:
       - id: reorder-python-imports
         args: [--unclassifiable-application-module=_msprime]
   - repo: https://github.com/asottile/pyupgrade
-    rev: v3.2.2
+    rev: v3.15.0
     hooks:
       - id: pyupgrade
         args: [--py38-plus]
   - repo: https://github.com/psf/black
-    rev: 22.10.0
+    rev: 23.9.1
     hooks:
       - id: black
         language_version: python3
   - repo: https://github.com/pycqa/flake8
-    rev: 5.0.4
+    rev: 6.1.0
     hooks:
       - id: flake8
         args: [--config=.flake8]
-        additional_dependencies: ["flake8-bugbear==22.10.27", "flake8-builtins==2.0.1"]
+        additional_dependencies: ["flake8-bugbear==23.9.16", "flake8-builtins==2.0.1"]
diff --git a/msprime/ancestry.py b/msprime/ancestry.py
index 2774d30ee..8a352d892 100644
--- a/msprime/ancestry.py
+++ b/msprime/ancestry.py
@@ -907,7 +907,7 @@ def _parse_sim_ancestry(
                 "If you wish to suppress this warning, you can use, e.g., "
                 "warnings.simplefilter('ignore', msprime.TimeUnitsMismatchWarning)"
             )
-            warnings.warn(message, TimeUnitsMismatchWarning)
+            warnings.warn(message, TimeUnitsMismatchWarning, stacklevel=2)
 
     if sequence_length is None:
         # These are all the cases in which we derive the sequence_length
diff --git a/msprime/cli.py b/msprime/cli.py
index 1b8007413..b517d0e2e 100644
--- a/msprime/cli.py
+++ b/msprime/cli.py
@@ -1027,13 +1027,15 @@ def run_simulate(args):
         "The simulate command is deprecated but will continue to work "
         "indefinitely. To simulate ancestries, please use the 'ancestry' "
         "command. To place mutations on a tree sequence, please use "
-        "the 'mutations' command."
+        "the 'mutations' command.",
+        stacklevel=2,
     )
 
     if args.compress:
         warnings.warn(
             "The --compress option is no longer supported and does nothing. "
-            "Please use the tszip utility to compress the output instead."
+            "Please use the tszip utility to compress the output instead.",
+            stacklevel=2,
         )
     tree_sequence = msprime.simulate(
         sample_size=int(args.sample_size),
diff --git a/msprime/demography.py b/msprime/demography.py
index 6af33fb17..493246838 100644
--- a/msprime/demography.py
+++ b/msprime/demography.py
@@ -1146,6 +1146,7 @@ def insert_extra_populations(self, tables):
                     "population names will not be recorded in the output "
                     "tree sequence",
                     IncompletePopulationMetadataWarning,
+                    stacklevel=2,
                 )
             if not description_in_metadata:
                 warnings.warn(
@@ -1153,6 +1154,7 @@ def insert_extra_populations(self, tables):
                     "population descriptions will not be recorded in the output "
                     "tree sequence",
                     IncompletePopulationMetadataWarning,
+                    stacklevel=2,
                 )
             left_out = []
             for population in self.populations[len(tables.populations) :]:
@@ -1173,12 +1175,14 @@ def insert_extra_populations(self, tables):
                     "in extra_metadata: these keys have not been recorded in the "
                     f"output tree sequence: {', '.join(left_out)}",
                     IncompletePopulationMetadataWarning,
+                    stacklevel=2,
                 )
         else:
             warnings.warn(
                 "No metadata schema present in population table, not recording "
                 "metadata",
                 IncompletePopulationMetadataWarning,
+                stacklevel=2,
             )
             # No metadata schema, just add bare populations.
             for _ in self.populations[len(tables.populations) :]:
@@ -1800,7 +1804,8 @@ def _from_old_style_map_populations(
                             warnings.warn(
                                 "Migration out of inactive population "
                                 f"({source}) to ({dest}). This may be an "
-                                "error in your model."
+                                "error in your model.",
+                                stacklevel=2,
                             )
 
             last_epoch_id_map = epoch_id_map
@@ -2671,9 +2676,9 @@ def epoch_resolve(deme, time):
         for time, events_group in itertools.groupby(
             resolved.events, operator.attrgetter("time")
         ):
-            events_group = list(events_group)
+            events_group_list = list(events_group)
             lineage_movements = []
-            for event in events_group:
+            for event in events_group_list:
                 if isinstance(event, LineageMovementEvent):
                     # Collect these so that we can later group them by source.
                     lineage_movements.extend(event._as_lineage_movements())
@@ -3747,7 +3752,8 @@ def _check_misspecification(self):
                     warnings.warn(
                         "Non-zero migration rates exist after merging "
                         f"population {k}. This almost certainly indicates "
-                        "demographic misspecification."
+                        "demographic misspecification.",
+                        stacklevel=2,
                     )
 
     def _populations_table(self, epoch, as_text=True):
@@ -4322,7 +4328,8 @@ def coalescence_rate_trajectory(
                 warnings.warn(
                     "Doubling the number of steps has resulted in different "
                     " predictions, please re-run with smaller step sizes to ensure "
-                    " numerical accuracy."
+                    " numerical accuracy.",
+                    stacklevel=2,
                 )
         return r, p_t
 
diff --git a/msprime/intervals.py b/msprime/intervals.py
index 52a0a085f..bccfb972c 100644
--- a/msprime/intervals.py
+++ b/msprime/intervals.py
@@ -689,6 +689,7 @@ def read_hapmap(cls, filename):
             "RecombinationMap.read_hapmap() is deprecated. "
             "Use RateMap.read_hapmap() instead.",
             FutureWarning,
+            stacklevel=2,
         )
         rate_map = RateMap.read_hapmap(filename, position_col=1, rate_col=2)
         # Mark anything missing as 0 for backwards compatibility. This will


Test patch:
diff --git a/tests/test_pedigree.py b/tests/test_pedigree.py
index 8cd2dcb4c..08a042a6c 100644
--- a/tests/test_pedigree.py
+++ b/tests/test_pedigree.py
@@ -70,7 +70,7 @@ def simulate_pedigree(
     for generation in range(1, num_generations):
         num_pairs = len(curr_gen) // 2
         if num_pairs == 0 and num_children_prob[0] != 1:
-            raise Exception(
+            raise ValueError(
                 f"Not enough people to make children in generation {generation}"
             )
         all_parents = rng.choice(curr_gen, size=(num_pairs, 2), replace=False)
@@ -341,9 +341,9 @@ def test_grandparents(self):
         assert set(tb[6].parents) == {4, 5}
 
     def test_insufficient_founders(self):
-        with pytest.raises(Exception):
+        with pytest.raises(ValueError):
             self.simple_sim(num_founders=1, num_children_prob=[0, 1])
-        with pytest.raises(Exception):
+        with pytest.raises(ValueError):
             self.simple_sim(num_founders=3, num_children_prob=[0, 1], num_generations=3)
 
     @pytest.mark.parametrize("num_children", range(5))