Skip to content

Commit

Permalink
Variable: Fix Variable.copy for StringVariable and TimeVariable
Browse files Browse the repository at this point in the history
Due to static constructors in Variable.copy and ContinuousVariable.copy
the StringVariable.copy and TimeVariable.copy used to return Variable
and ContinuousVariable instances respectively.

Fix this by dispatching on `type(self)`.
  • Loading branch information
ales-erjavec committed Sep 23, 2016
1 parent a291589 commit 6aaf362
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Orange/data/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from math import isnan, floor, sqrt
import numpy as np
from pickle import PickleError
import copy

import collections
from datetime import datetime, timedelta, timezone
Expand Down Expand Up @@ -421,7 +420,7 @@ def __reduce__(self):
return make_variable, (self.__class__, self._compute_value, self.name), self.__dict__

def copy(self, compute_value):
var = Variable(self.name, compute_value)
var = type(self)(self.name, compute_value=compute_value)
var.attributes = dict(self.attributes)
return var

Expand Down Expand Up @@ -518,7 +517,7 @@ def repr_val(self, val):
str_val = repr_val

def copy(self, compute_value=None):
var = ContinuousVariable(self.name, self.number_of_decimals, compute_value)
var = type(self)(self.name, self.number_of_decimals, compute_value)
var.attributes = dict(self.attributes)
return var

Expand Down Expand Up @@ -899,6 +898,12 @@ def __init__(self, *args, **kwargs):
self.have_date = 0
self.have_time = 0

def copy(self, compute_value=None):
copy = super().copy(compute_value=compute_value)
copy.have_data = self.have_date
copy.have_time = self.have_time
return copy

@staticmethod
def _tzre_sub(s, _subtz=re.compile(r'([+-])(\d\d):(\d\d)$').sub):
# Replace +ZZ:ZZ with ISO-compatible +ZZZZ, or strip +0000
Expand Down
1 change: 1 addition & 0 deletions Orange/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_copy_copies_attributes(self):
var.attributes["a"] = "b"
var2 = var.copy(compute_value=None)
self.assertIn("a", var2.attributes)
self.assertIsInstance(var2, type(var))

var2.attributes["a"] = "c"
# Attributes of original value should not change
Expand Down

0 comments on commit 6aaf362

Please sign in to comment.