# Type Assertion and built in Ignition Types

**URL:** <https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [March 12, 2018, 6:45pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330 "2018-03-12T18:45:30Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![SeraSera](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SeraSera](https://forum.inductiveautomation.com/u/SeraSera)\
**Post date:** [March 12, 2018, 6:45pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/1 "2018-03-12T18:45:30Z")

</div>

Hello All!

I’m looking at checking for a specific type of class within a Scripting package in Python and would like some error checking and type checking built-in (at least during development)

So in the past I would use assert and isType for this purpose, making sanity checks for passed objects to the library.

I’m running into a wall when trying to specify PyDataSets as types though.

```auto
maquinas = system.db.runPrepQuery(machq, [2],'tablas')
listo = []

print type(listo)
if type(listo) == list:
	print "List!!"	
else:
	print "Not a list!"

print type(maquinas)
if type(maquinas) == com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities$PyDataSet:
	print "Machine!!"	
else:
	print "Not a Machine!!"

```

The second example seems to break the function.

Any direction would be great.I can definitely work around this (and I will) but it’s going to be a nagging voice in the back of my head I’d like to quell.

---

<div class="post-metadata">

**Author:** ![phillip.bow](https://avatars.discourse-cdn.com/v4/letter/p/ce73a5/32.png) [@phillip.bow](https://forum.inductiveautomation.com/u/phillip.bow)\
**Post date:** [March 12, 2018, 8:07pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/2 "2018-03-12T20:07:42Z")

</div>

Try:

```auto
from com.inductiveautomation.ignition.common.script.builtin import DatasetUtilities
maquinas = system.db.runQuery("SELECT 1", "")
listo = []

print type(listo)
if type(listo) == list:
	print "List!!"	
else:
	print "Not a list!"

if type(maquinas) == DatasetUtilities.PyDataSet:
	print "Machine!!"	
else:
	print "Not a Machine!!"

```

---

<div class="post-metadata">

**Author:** ![SeraSera](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SeraSera](https://forum.inductiveautomation.com/u/SeraSera)\
**Post date:** [March 12, 2018, 8:29pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/3 "2018-03-12T20:29:09Z")

</div>

No dice.

It can’t resolve the “DatasetUtilities” typepath.

---

<div class="post-metadata">

**Author:** ![phillip.bow](https://avatars.discourse-cdn.com/v4/letter/p/ce73a5/32.png) [@phillip.bow](https://forum.inductiveautomation.com/u/phillip.bow)\
**Post date:** [March 12, 2018, 8:35pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/4 "2018-03-12T20:35:04Z")

</div>

It is case-sensitive so if you have “DataSetUtilities” it’ll fail to resolve. Should be “DatasetUtilities”.

Otherwise it could be a version issue, I tested on 7.9.6.

---

<div class="post-metadata">

**Author:** ![SeraSera](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SeraSera](https://forum.inductiveautomation.com/u/SeraSera)\
**Post date:** [March 12, 2018, 8:39pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/5 "2018-03-12T20:39:10Z")

</div>

My apologies, I had it typed right in the machine console.

```auto
NameError: name 'DatasetUtilities' is not defined

```

---

<div class="post-metadata">

**Author:** ![phillip.bow](https://avatars.discourse-cdn.com/v4/letter/p/ce73a5/32.png) [@phillip.bow](https://forum.inductiveautomation.com/u/phillip.bow)\
**Post date:** [March 12, 2018, 8:44pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/6 "2018-03-12T20:44:26Z")

</div>

And the import is in there within the scope of your script?

I believe it will fail at the import call with a different message if it truly cannot find the class within the class path.

Something like:  
ImportError: cannot import name DatasetUtilities

---

<div class="post-metadata">

**Author:** ![SeraSera](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SeraSera](https://forum.inductiveautomation.com/u/SeraSera)\
**Post date:** [March 12, 2018, 8:48pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/7 "2018-03-12T20:48:30Z")

</div>

I haven’t imported it… I will try that and follow up later today.

Thanks!

---

<div class="post-metadata">

**Author:** ![SeraSera](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SeraSera](https://forum.inductiveautomation.com/u/SeraSera)\
**Post date:** [March 12, 2018, 11:55pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/8 "2018-03-12T23:55:23Z")

</div>

Nope! Throws an error, saying it can’t be found just the same.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 13, 2018, 12:27am UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/9 "2018-03-13T00:27:46Z")

</div>

What version of Ignition are you using? The class `com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities` has existed at least since v7.7.

---

<div class="post-metadata">

**Author:** ![SeraSera](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SeraSera](https://forum.inductiveautomation.com/u/SeraSera)\
**Post date:** [March 13, 2018, 12:55am UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/10 "2018-03-13T00:55:37Z")

</div>

Ayup! That did the trick.

Not used to having to give the verbose version of the class name, but I suppose Jython forces the issue.

Thanks pturmel!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 13, 2018, 12:58pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/11 "2018-03-13T12:58:38Z")

</div>

> [@SeraSera](#):
>
> Not used to having to give the verbose version of the class name, but I suppose Jython forces the issue.

In what language can you avoid spelling out the class/package name, at least as an import?  
{ Hint: Look at the first line of phillip.bow's suggested code. }

---

<div class="post-metadata">

**Author:** ![SeraSera](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@SeraSera](https://forum.inductiveautomation.com/u/SeraSera)\
**Post date:** [March 13, 2018, 4:23pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/12 "2018-03-13T16:23:47Z")

</div>

No I meant on the type assertion. I had typed the entire package and that was what made it work.

The first line that had

`from XXXXX(*32) import YYYY`

It didn’t have the color call out I’m used to reading for imports, so I glanced through it as text.

---

<div class="post-metadata">

**Author:** ![martinel37461](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@martinel37461](https://forum.inductiveautomation.com/u/martinel37461)\
**Post date:** [March 13, 2018, 6:32pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/13 "2018-03-13T18:32:29Z")

</div>

I use an empty dataset and type to type comparison which eliminates the need for the lengthy spelling or imports and hopefully will survive updates to root object types.

> if **type** (possibleDatasetSource) == **type** ( system.dataset.toDataSet(,) ):  
>  print 'you have a dataset'  
> else:  
>  print 'not a dataset'

Please let me know if this is a bad practice.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 13, 2018, 6:50pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/14 "2018-03-13T18:50:11Z")

</div>

> [@martinel37461](#):
>
> Please let me know if this is a bad practice.

It is a bad practice. Your sample will only match a BasicDataset, the output type of system.dataset.toDataSet(). There are other classes that implement the Dataset interface that Ignition can deliver at any point. It's also slow as death as you are creating and throwing away an empty dataset every time you check.

---

<div class="post-metadata">

**Author:** ![martinel37461](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@martinel37461](https://forum.inductiveautomation.com/u/martinel37461)\
**Post date:** [March 13, 2018, 7:52pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/15 "2018-03-13T19:52:36Z")

</div>

That's good to know thanks!

Actually, in my code, I have a reliable, known dataset variable of the type I'm expecting to get so I don't have the slowness of creating and throwing away a dataset but that will not always be the case.

I've fiddled with several different options from other posts but can't seem to get the right syntax so the type compare has bailed me out.

I've copied from this thread:

>  from com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities import DatasetUtilities

but get:

> ImportError: cannot import name DatasetUtilities

When I import the entire library I still get errors:

> import com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities  
> try:  
>  if type(dsType) == DataSetUtilities.PyDataSet:  
>   print 'DataSetUtilities worked'  
> except:  
>  print 'error using DataSetUtilities.PyDataSet'  
> try:  
>  if type(dsType) == DatasetUtilities.PyDataSet:  
>   print 'DatasetUtilities worked'  
> except:  
>   print 'error using DatasetUtilities.PyDataSet'

I appreciate the simple type validation of other data types, e.g.

> d = {'abc':123,'xyz':789}  
> if type(d) == dict:  
>  print 'you have a dict'  
> l = ['a','b','c']  
> if type(l) == list:  
>  print 'you have a list'

and I would really appreciate an actual working example that would help me know when I have a legitimate dataset.

Thanks

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 13, 2018, 7:54pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/16 "2018-03-13T19:54:58Z")

</div>

> [@martinel37461](#):
>
> from com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities import DatasetUtilities

You put "DatasetUtilities" twice. Try copying and pasting the first line of phillip.bow's code.

---

<div class="post-metadata">

**Author:** ![martinel37461](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@martinel37461](https://forum.inductiveautomation.com/u/martinel37461)\
**Post date:** [March 13, 2018, 8:52pm UTC](https://forum.inductiveautomation.com/t/type-assertion-and-built-in-ignition-types/17330/17 "2018-03-13T20:52:45Z")

</div>

Great, **Thank You!**  
This is what works:

> from com.inductiveautomation.ignition.common.script.builtin import DatasetUtilities  
> from com.inductiveautomation.ignition.common import BasicDataset

> basicdataset = system.dataset.toDataSet(,)  
> pidataset = system.dataset.toPyDataSet(basicdataset)  
> print type(basicdataset)  
> print type(pidataset)  
> if type(basicdataset) == BasicDataset:  
>  print 'Successfully detected a BasicDataset'  
> if type(pidataset) == DatasetUtilities.PyDataSet   
>  print 'Successfully detected a PyDataSet'
